Sometimes it could be necessary to get time with microseconds part. Especially it is good for something like logging when multiple entries are recorded per second and preferably to know how they are spread out.
At first glance everything looked very easy:
print date('Y-m-d H:i:s.u') ; |
print date('Y-m-d H:i:s.u') ;
where u is microseconds.
However it turns out PHP manual has a tiny note which states
Since this [date()] function only accepts integer timestamps the u format character is only useful when using the date_format() function with user based timestamps created with date_create().
Which means that u kind of works but in case of calling within date() function the result is always .000000.
So the straight forward solution could be something like this:
print date('Y-m-d H:i:s') . substr((string)microtime(), 1, 8); |
print date('Y-m-d H:i:s') . substr((string)microtime(), 1, 8);
But the drawback of the solution above is two calls to the time related functions which potentially may cause some accuracy issues.
Thus even better solution is doing just one call of time function:
list($usec, $sec) = explode(' ', microtime());
print date('Y-m-d H:i:s', $sec) . $usec; |
list($usec, $sec) = explode(' ', microtime());
print date('Y-m-d H:i:s', $sec) . $usec;