## Get current time in milliseconds since the midnight of today

Moderators: egami, macek, gesf

Americanhorizon
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm
Hi

I need to retrieve the current time in milliseconds since the midnight of the current day.
I've found some solutions such this:

Code: Select all

`````` <?php

function timeAndMilliseconds()
{
\$m = explode(' ',microtime());
return array(\$m[1], (int)round(\$m[0]*1000,3));
}

list(\$totalSeconds, \$extraMilliseconds) = timeAndMilliseconds();
echo date("H:i:s", \$totalSeconds) . ".\$extraMilliseconds\n";

?> ``````
It retrive me something like 12:36:13.383
At this point is easy to calculate che milliseconds...

But i want to know if exist an simplest way to do this with few passages..
I know the microtime() function, but according to the official documentation, it retrieve me the number of milliseconds elapsed since the Unix epoch (0:00:00 January 1, 1970 GMT).. while i need milliseconds since the midnight of current day.

In actionscript 3 i've already done something of genere, calculating first the microtime respect to current time, and after the microtime respect to midnight of current day. The difference between the two is the number that i searching for..

I've to do something like this in php too?

Thanks

egami
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT
\$today = mktime (0,0,0,date('m'),date('d'),date('Y'));

This gives you the time in seconds of 12:00am today.

\$rightnow = date('U');
\$diff =\$rightnow - \$today;
\$totaltimeinmiliseconds = \$diff * 1000;

Americanhorizon
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm
egami wrote:\$today = mktime (0,0,0,date('m'),date('d'),date('Y'));

This gives you the time in seconds of 12:00am today.
This is ok, because the midnight of today is an "integer" value
\$rightnow = date('U');
this is wrong for what i've to do, because i need to know with precision actual milliseconds.. This method instead retrieve me a sort of "Math.floor" of the actual seconds, without consider the milliseconds elapsed (40.535 or 40.786 etc etc....)
Do you understand me?

Sorry for my english, i'm italian.

egami
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT
php.net/microtime

Americanhorizon
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm
such you can see from my first post, i already know the microtime function, so just confrim what i've supposed from the beginning

Americanhorizon
New php-forum User
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm
but why in this string it multiplies for 1000,3 ? What's the ",3" ?

return array(\$m[1], (int)round(\$m[0]*1000,3));