views:

756

answers:

6

I have a total ammount of milliseconds (ie 70370) and I want to display it as minutes:seconds:milliseconds ie 00:00:0000.

How can I do this in PHP?

+1  A: 

Try this function to display amount of milliseconds the way you like:

<?php
function udate($format, $utimestamp = null)
{
   if (is_null($utimestamp)) {
       $utimestamp = microtime(true);
   }

   $timestamp = floor($utimestamp);
   $milliseconds = round(($utimestamp - $timestamp) * 1000000);

   return date(preg_replace('`(?<!\\\\)u`', sprintf("%06u", $milliseconds), $format), $timestamp);
}

echo udate('H:i:s.u'); // 19:40:56.78128
echo udate('H:i:s.u', 654532123.04546); // 16:28:43.045460
?>

Source

Kamil Szot
@Kamil - I inlined the code so it's here for posterity.
Dominic Rodger
btw - I have no idea what that `preg_replace` is doing. Looks pretty odd.
Dominic Rodger
@Dominic - Thank you. I'll do that next time.
Kamil Szot
I guess it just replaces u not preceded by backslash with actual number.
Kamil Szot
If I were a math teacher I would say if $utime = NULL, the $milliseconds will return a negativ value?Why do you multiple the milliseconds with 100000 ? If you want to have seconds, minutes etc. you have to divise it.
daemonfire300
I'd make sure the milliseconds part is formatted with leading zero's though, using something like `sprintf("%06u", $milliseconds)`, otherwise 5 milliseconds will look like 0.5
Wim
@Wim - I think you are right. Author of this code made a mistake. It's visible even in example he gave. I'm correcting this answer accordingly to your comment to avoid misleading future viewers.
Kamil Szot
A: 

I believe there's no built-in function for formatting miliseconds in PHP, you'll need to use maths.

Alan Storm
A: 

As mentioned in the manual:

u Microseconds (added in PHP 5.2.2) Example: 654321

We have a 'u' parameter for the date() function

Example:

if(($u/60) >= 60)
{
$u = mktime(0,($u / 360));
}
date('H:i:s',$u);
daemonfire300
+2  A: 

If you are using PHP 5.3 you can make use of the DateInterval object:

list($seconds, $millis) = explode('.', $milliseconds / 1000);
$range = new DateInterval("PT{$seconds}S");
echo $range->format('%H:%I:%S') . ':' . str_pad($millis, 3, '0', STR_PAD_LEFT);
soulmerge
How do you get $seconds? You have to convert $millis into $seconds, don't you?
daemonfire300
@daemonfire: Thx for pointing out. Should work now.
soulmerge
+3  A: 

Don't fall into the trap of using date functions for this! What you have here is a time interval, not a date. The naive approach is to do something like this:

date("h:i:s.u", $mytime / 1000)

but because the date function is used for (gasp!) dates, it doesn't handle time the way you would want it to in this situation - it takes timezones and daylight savings, etc, into account when formatting a date/time.

Instead, you will probably just want to do some simple maths:

$input = 70135;

$uSec = $input % 1000;
$input = floor($input / 1000);

$seconds = $input % 60;
$input = floor($input / 60);

$minutes = $input % 60;
$input = floor($input / 60); 

// and so on, for as long as you require.
nickf
We were wrong, you are right ;) ++1
daemonfire300
It would still be a hack, but you can use `gmdate()` if you are sure that your values will be less than 24 hours.
soulmerge
Perfect! Thanks a million!
unidev
+1  A: 

why bother with date() and formatting when you can just use math ? if $ms is your number of miliseconds

echo floor($ms/60000).':'.floor(($ms%60000)/1000).':'.str_pad(floor($ms%1000),3,'0', STR_PAD_LEFT);
jab11