Reputation: 3525
What I want to get is the very like time()
,but should be accurate in ms:
2010-11-15 21:21:00:987
Is that possible in PHP?
Upvotes: 2
Views: 29171
Reputation: 816
Since PHP 7.3 hrtime
has been available.
This means you can use high resolution timers in nanoseconds without any of the issues of microtime. (e.g. System clock changing between tests)
So, you can now get ms reliably on 64bit platforms with :
intval( hrtime(true) / 1000000 ); //Convert nanosecond to ms
Format as described in earlier answers.
Upvotes: 1
Reputation: 95
This is my way
list($usec, $sec) = explode(" ", microtime());
$time = date("Y-m-d H:i:s:",$sec).intval(round($usec*1000));
echo $time;
Upvotes: 1
Reputation: 168843
Use the microtime()
function.
See the manual page here: http://php.net/manual/en/function.microtime.php
[EDIT] To get the output in year/month/day/hour/minutes/seconds/ms as requested:
Try something like this:
list($usec, $sec) = explode(" ", microtime());
$output = date('Y/m/d H:i:s',$sec). " /" . $usec;
Again, see the manual page for more details on how microtime()
works.
Upvotes: 2
Reputation: 294
function udate($format, $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
echo udate('Y-m-d H:i:s:u'); // 2010-11-15 21:21:00:987
Upvotes: 10
Reputation: 318748
Use microtime and convert it to milliseconds:
$millitime = round(microtime(true) * 1000);
Upvotes: 8