Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Get current time in milliseconds since the midnight of today

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Get current time in milliseconds since the midnight of today

Postby Americanhorizon » Tue Sep 11, 2012 2:42 am

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
Americanhorizon
New php-forum User
New php-forum User
 
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Re: Get current time in milliseconds since the midnight of t

Postby egami » Tue Sep 11, 2012 6:54 am

$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;
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Get current time in milliseconds since the midnight of t

Postby Americanhorizon » Wed Sep 12, 2012 3:03 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.
Americanhorizon
New php-forum User
New php-forum User
 
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Re: Get current time in milliseconds since the midnight of t

Postby egami » Thu Sep 13, 2012 11:05 am

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

Re: Get current time in milliseconds since the midnight of t

Postby Americanhorizon » Fri Sep 14, 2012 9:50 am

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
New php-forum User
 
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm

Re: Get current time in milliseconds since the midnight of t

Postby Americanhorizon » Sat Sep 15, 2012 6:27 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));
Americanhorizon
New php-forum User
New php-forum User
 
Posts: 16
Joined: Sun Dec 18, 2011 1:20 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 6 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.