this code always returns 0 in PHP 5.2.5 for microseconds:
format(\"Y-m-d\\TH:i:s.u\") . \"\\n\";
?>
Right, I'd like to clear this up once and for all.
An explanation of how to display the ISO 8601 format date & time in PHP with milliseconds and microseconds...
milliseconds or 'ms' have 4 digits after the decimal point e.g. 0.1234. microseconds or 'µs' have 7 digits after decimal. Seconds fractions/names explanation here
PHP's date()
function does not behave entirely as expected with milliseconds or microseconds as it will only except an integer, as explained in the php date docs under format character 'u'.
Based on Lucky's comment idea (here), but with corrected PHP syntax and properly handling seconds formatting (Lucky's code added an incorrect extra '0' after the seconds)
These also eliminate race conditions and correctly formats the seconds.
Working Equivalent of date('Y-m-d H:i:s').".$milliseconds";
list($sec, $usec) = explode('.', microtime(true));
echo date('Y-m-d H:i:s.', $sec) . $usec;
Output = 2016-07-12 16:27:08.5675
Working Equivalent of date('Y-m-d H:i:s').".$microseconds";
or date('Y-m-d H:i:s.u')
if the date function behaved as expected with microseconds/microtime()
/'u'
list($usec, $sec) = explode(' ', microtime());
echo date('Y-m-d H:i:s', $sec) . substr($usec, 1);
Output = 2016-07-12 16:27:08.56752900
This has worked for me and is a simple three-liner:
function udate($format='Y-m-d H:i:s.', $microtime=NULL) {
if(NULL === $microtime) $microtime = microtime();
list($microseconds,$unix_time) = explode(' ', $microtime);
return date($format,$unix_time) . array_pop(explode('.',$microseconds));
}
This, by default (no params supplied) will return a string in this format for the current microsecond it was called:
YYYY-MM-DD HH:MM:SS.UUUUUUUU
An even simpler/faster one (albeit, with only half the precision) would be as follows:
function udate($format='Y-m-d H:i:s.', $microtime=NULL) {
if(NULL === $microtime) $microtime = microtime(true);
list($unix_time,$microseconds) = explode('.', $microtime);
return date($format,$unix_time) . $microseconds;
}
This one would print out in the following format:
YYYY-MM-DD HH:MM:SS.UUUU
String in a format accepted by strtotime() It work!
Try this and it shows micro seconds:
$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro,$t) );
print $d->format("Y-m-d H:i:s.u");
PHP documentation clearly says "Note that date() will always generate 000000 since it takes an integer parameter...". If you want a quick replacement for date()
function use below function:
function date_with_micro($format, $timestamp = null) {
if (is_null($timestamp) || $timestamp === false) {
$timestamp = microtime(true);
}
$timestamp_int = (int) floor($timestamp);
$microseconds = (int) round(($timestamp - floor($timestamp)) * 1000000.0, 0);
$format_with_micro = str_replace("u", $microseconds, $format);
return date($format_with_micro, $timestamp_int);
}
(available as gist here: date_with_micro.php)
date('u') is supported only from PHP 5.2. Your PHP may be older!