Milliseconds to HH:MM:SS ??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunbin
    New Member
    • Feb 2007
    • 7

    Milliseconds to HH:MM:SS ??

    hi,

    How can i get the FLV file or any type of video file's duration in HH:MM:SS or HH:MM:SS:FF format from milliosecond ? I already got duration in millisecond.

    PHP script needed ... Plz help me !

    Sunbin
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    We do not post code scripts here, we help members with the development of their code. So this you can script this yourself. Hint:
    Code:
    divide your field by milliseconds per hour (1000*60*60) => hrs
    divide rest by milliseconds per minute (1000*60)        => mins
    divide rest by milliseconds per second (1000)           => secs
    remains                                                 => millisecs
    Ronald :cool:

    Comment

    • sunbin
      New Member
      • Feb 2007
      • 7

      #3
      Thanks Ronverdonk,

      Finally, i developed a script block to convert millisecond into HH:MM:SS format. I develped this script to display a video file (.flv) duration in that format.

      My script blcok is :
      [php]
      <?php
      //milliseconds to hh:mm:ss by <sunbin>sunbin. np@gmail.com
      $miliseconds=90 0000;
      $seconds= $miliseconds/1000;
      //for seconds
      if($seconds> 0)
      {
      $sec= "" . ($seconds%60);
      if($seconds % 60 <10)
      {
      $sec= "0" . ($seconds%60);
      }
      }
      //for mins
      if($seconds > 60)
      {
      $mins= "". ($seconds/60%60);
      if(($seconds/60%60)<10)
      {
      $mins= "0" . ($seconds/60%60);
      }
      }
      else
      {
      $mins= "00";
      }
      //for hours
      if($seconds/60 > 60)
      {
      $hours= "". ($seconds/60/60);
      if(($seconds/60/60) < 10)
      {
      $hours= "0" . ($seconds/60/60);
      }

      }
      else
      {
      $hours= "00";
      }

      print $time_format= "" . $hours . ":" . $mins . ":" . $sec; //00:15:00

      ?>[/php]


      - Sunbin

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        This is one way of doing it, but it is a bit more lengthy than is needed. In the next sample I show you how you can do that a bit easier. But both methods start at the high end: begin to calculate the hours and then go downwards, as I showed you in my previous post.

        Method 1 calculates every value in a separate variable, and is outputted when all values have been calculated.

        Method 2 is a lot shorter, it displays the calculation direct to the screen. And when you want it shorter still, you can use the constant values directly in the method 2, but that makes it harder to read.

        [php]<?php
        // ------------------------------------------------
        // Calculate HH:MM:SS:TH from time in milliseconds
        // ------------------------------------------------

        // setup the test time 13:44:21:33
        $mytime = (1000 * 60 * 60 * 13) + (1000 * 60 * 44) + (1000 * 21) + 333; initialize the constants
        $msec_hh = 1000 * 60 * 60; // millisecs per hour
        $msec_mm = 1000 * 60; // millisecs per minute
        $msec_ss = 1000; // millisecs per second

        //----------------------------
        // Method 1 indirect
        // ---------------------------
        // calculate HH:MM:SS:TH
        $hh = $mytime / $msec_hh; // divide by millisecs per hour => hrs
        $r = $mytime % $msec_hh;
        $mm = $r / $msec_mm; // divide rest by millisecs per minute => mins
        $r = $r % $msec_mm;
        $ss = $r / $msec_ss; // divide rest by millisecs per second => secs
        $r = $r % $msec_ss;
        echo sprintf("Durati on is %02s:%02s:%02s: %02s", (int)$hh, (int)$mm, (int)$ss, (int)$r);

        //----------------------------
        //method 2 direct
        //----------------------------
        echo sprintf("<br>Du ration is %02s:%02s:%02s: %02s",
        (int)($mytime / $msec_hh),
        (int)(($mytime % $msec_hh) / $msec_mm),
        (int)((($mytime % $msec_hh) % $msec_mm) / $msec_ss),
        (int)((($mytime % $msec_hh) % $msec_mm) % $msec_ss) );
        ?>
        [/php]

        Ronald :cool:

        Comment

        • sunbin
          New Member
          • Feb 2007
          • 7

          #5
          ThanX ronverdonk for ur ideas !

          I expect the same again and again !

          Sunbin

          Comment

          Working...