Create 3 digit Millisecond with php

Smith picture Smith · Feb 11, 2012 · Viewed 14.3k times · Source

I have 13 digit number and want to create date and time with include milisecond

Example code is like this this is my php script

$mil = 1328910295939;
$seconds = $mil / 1000;
$showdate = date('Y:m:d H:i:s', $seconds) ;

echo "$showdate";

the result is like this 2012:02:10 15:44:55.xxx ===> xxx is 3 digit miliseconds that i want to show up.

and how to include with 3 digit milisecond after H:i:s

Please help me.....

Answer

Westie picture Westie · Feb 11, 2012

How about something like this?

$mil = 1328910295939;

function toTimestamp($milliseconds)
{
    $seconds = $milliseconds / 1000;
    $remainder = round($seconds - ($seconds >> 0), 3) * 1000;

    return date('Y:m:d H:i:s.', $seconds).$remainder;
}

echo toTimestamp($mil);

Tadaa!

It should be pretty quick too.

Also, this is the output: 2012:02:10 15:44:55.939 - why you're not using - for delimiting the date portion beats me.