seconds converted to HH-MM-SS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kription

    seconds converted to HH-MM-SS

    Hello,

    I am having a nightmare of a time getting seconds converted to
    hour-minute-seconds

    I have a third party application that outputs only in seconds since

    so if someone has been logged in for 1 minute, the time value would be 60,
    if they were on for 5 minutes and 3 seconds the value would be 303

    what I need is a way to take the 303 seconds and convert it to a human
    readable:
    0 hours 5 minutes and 3 seconds

    and if possible; if hours = 0 then it wouldn't display - the same for
    minutes.

    Thank you in advance !!

    -L"k"
    *noob*


  • CountScubula

    #2
    Re: seconds converted to HH-MM-SS

    "Kription" <kription@cox.n et> wrote in message
    news:mj_Pb.4702 $Ue.2767@lakere ad03...[color=blue]
    > Hello,
    >
    > I am having a nightmare of a time getting seconds converted to
    > hour-minute-seconds
    >
    > I have a third party application that outputs only in seconds since
    >
    > so if someone has been logged in for 1 minute, the time value would be 60,
    > if they were on for 5 minutes and 3 seconds the value would be 303
    >
    > what I need is a way to take the 303 seconds and convert it to a human
    > readable:
    > 0 hours 5 minutes and 3 seconds
    >
    > and if possible; if hours = 0 then it wouldn't display - the same for
    > minutes.
    >
    > Thank you in advance !!
    >
    > -L"k"
    > *noob*
    >
    >[/color]

    try this:

    $t = (5 * 60) + 3; // 5 minutes and 3 seconds
    print date("H:i:s",-57600 + $t);

    will print "00:05:03"

    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools


    Comment

    • Pedro Graca

      #3
      Re: seconds converted to HH-MM-SS

      Kription wrote:[color=blue]
      > I am having a nightmare of a time getting seconds converted to
      > hour-minute-seconds[/color]
      (snip)[color=blue]
      > what I need is a way to take the 303 seconds and convert it to a human
      > readable:
      > 0 hours 5 minutes and 3 seconds
      >
      > and if possible; if hours = 0 then it wouldn't display - the same for
      > minutes.[/color]

      #v+
      <?php
      function secs2hms($secs) {
      if ($secs<0) return false;
      $m = (int)($secs / 60); $s = $secs % 60;
      $h = (int)($m / 60); $m = $m % 60;
      return array($h, $m, $s);
      }

      for ($i=0; $i<16; ++$i) {
      echo implode(', ', secs2hms($i*817 )), "\n";
      }
      ?>
      #v-

      What this script doesn't do, I'll leave up to you :)

      Happy Coding :)
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Robert Downes

        #4
        Re: seconds converted to HH-MM-SS

        Kription wrote:[color=blue]
        >
        > what I need is a way to take the 303 seconds and convert it to a human
        > readable:
        > 0 hours 5 minutes and 3 seconds[/color]

        Well, I wrote this yesterday. Does the job, but I'd be very interested
        to hear what people think about efficiency and processor cost. Is a
        funciton like this wildly unreasonable?



        // timePassed is a function to turn a time into a more human-friendly format
        // Provide it with the number of seconds that have passed, and it will
        return a more suitable
        // description of how much time has passed.

        function timePassed($pas tTimestamp)
        {
        $currentTimesta mp = time();
        $timePassed = $currentTimesta mp - $pastTimestamp; //time passed in seconds
        // Minute == 60 seconds
        // Hour == 3600 seconds
        // Day == 86400
        // Week == 604800
        $elapsedString = "";
        if($timePassed > 604800)
        {
        $weeks = floor($timePass ed / 604800);
        $timePassed -= $weeks * 604800;
        $elapsedString = $weeks." weeks, ";
        }
        if($timePassed > 86400)
        {
        $days = floor($timePass ed / 86400);
        $timePassed -= $days * 86400;
        $elapsedString .= $days." days, ";
        }
        if($timePassed > 3600)
        {
        $hours = floor($timePass ed / 3600);
        $timePassed -= $hours * 3600;
        $elapsedString .= $hours." hours, ";
        }
        if($timePassed > 60)
        {
        $minutes = floor($timePass ed / 60);
        $timePassed -= $minutes * 60;
        $elapsedString .= $minutes." minutes, ";
        }
        $elapsedString .= $timePassed." seconds";

        return $elapsedString;
        }



        If you think it's what you're looking for, feel free to use it.
        --
        Bob
        London, UK
        echo Mail fefsensmrrjyahe eoceoq\! | tr "jefroq\!" "@obe.uk"

        Comment

        • Chung Leong

          #5
          Re: seconds converted to HH-MM-SS

          Use gmdate() instead. The extra hours that you're getting comes from the
          time zone difference between your and Greenwich, and would change depending
          on where you are (I got error from your code, for instance, because the
          timestamp was negative).

          gmdate("H:i:s", $t) will yield exactly what's needed.

          Uzytkownik "CountScubu la" <me@scantek.hot mail.com> napisal w wiadomosci
          news:Cv_Pb.1471 1$ex2.8540@news svr25.news.prod igy.com...
          [color=blue]
          > $t = (5 * 60) + 3; // 5 minutes and 3 seconds
          > print date("H:i:s",-57600 + $t);
          >
          > will print "00:05:03"
          >
          > --
          > Mike Bradley
          > http://www.gzentools.com -- free online php tools
          >
          >[/color]


          Comment

          • Kription

            #6
            Re: seconds converted to HH-MM-SS

            Thank you..

            Doh.. Guess who feels dumb now ! - it was so simple, I should have known
            better.

            -L"k"

            "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
            news:6JqdnUcf6r lEIYzdRVn-hQ@comcast.com. ..[color=blue]
            > Use gmdate() instead. The extra hours that you're getting comes from the
            > time zone difference between your and Greenwich, and would change
            > depending
            > on where you are (I got error from your code, for instance, because the
            > timestamp was negative).
            >
            > gmdate("H:i:s", $t) will yield exactly what's needed.
            >
            > Uzytkownik "CountScubu la" <me@scantek.hot mail.com> napisal w wiadomosci
            > news:Cv_Pb.1471 1$ex2.8540@news svr25.news.prod igy.com...
            >[color=green]
            >> $t = (5 * 60) + 3; // 5 minutes and 3 seconds
            >> print date("H:i:s",-57600 + $t);
            >>
            >> will print "00:05:03"
            >>
            >> --
            >> Mike Bradley
            >> http://www.gzentools.com -- free online php tools
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • CountScubula

              #7
              Re: seconds converted to HH-MM-SS

              "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
              news:6JqdnUcf6r lEIYzdRVn-hQ@comcast.com. ..[color=blue]
              > Use gmdate() instead. The extra hours that you're getting comes from the
              > time zone difference between your and Greenwich, and would change[/color]
              depending[color=blue]
              > on where you are (I got error from your code, for instance, because the
              > timestamp was negative).
              >
              > gmdate("H:i:s", $t) will yield exactly what's needed.[/color]

              dont you just hate it when it right under your knose?

              --
              Mike Bradley
              http://www.gzentools.com -- free online php tools


              Comment

              Working...