By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Milliseconds to HH:MM:SS ??

sunbin
P: 7
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
Feb 11 '07 #1
Share this Question
Share on Google+
4 Replies


ronverdonk
Expert 2.5K+
P: 4,258
We do not post code scripts here, we help members with the development of their code. So this you can script this yourself. Hint:
Expand|Select|Wrap|Line Numbers
  1. divide your field by milliseconds per hour (1000*60*60) => hrs
  2. divide rest by milliseconds per minute (1000*60)        => mins
  3. divide rest by milliseconds per second (1000)           => secs
  4. remains                                                 => millisecs
Ronald :cool:
Feb 11 '07 #2

sunbin
P: 7
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=900000;
$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
Feb 16 '07 #3

ronverdonk
Expert 2.5K+
P: 4,258
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("Duration is %02s:%02s:%02s:%02s", (int)$hh, (int)$mm, (int)$ss, (int)$r);

//----------------------------
//method 2 direct
//----------------------------
echo sprintf("<br>Duration 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:
Feb 16 '07 #4

sunbin
P: 7
ThanX ronverdonk for ur ideas !

I expect the same again and again !

Sunbin
Feb 19 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.