In PHP, getting the microsecond part of the current time is usually used to use the gettimeofday() function. This function can return the seconds and microseconds of the current time, and the accuracy should be very high. However, in actual use, many developers find that the accuracy of the microsecond part is not as expected, and there are losses or inaccuracies, resulting in inaccurate time-related calculations, especially in scenarios with extremely high performance or timestamp requirements.
This article will analyze the reasons for the loss of gettimeofday() accuracy in depth and provide several effective solutions to solve the problem of microsecond inaccuracy in PHP.
gettimeofday() returns an array containing two key fields:
sec : seconds since the Unix epoch
usec : the number of microseconds in the current second
Under normal circumstances, usec should be accurate to one millionth of a second, but in some systems or PHP versions, usec is not completely reliable, and may have low accuracy or even zero.
<?php
$time = gettimeofday();
echo "Seconds: " . $time['sec'] . "\n";
echo "Microseconds: " . $time['usec'] . "\n";
?>
If you find in your test that usec is always zero, or the change is very small, it is likely to be a system call level problem or a limitation of the PHP binding function itself.
microtime() is a more commonly used function in PHP to get microsecond timestamps. Calling microtime(true) returns a decimal seconds timestamp with microseconds.
<?php
$microtime = microtime(true);
echo "Current time with microseconds: " . $microtime . "\n";
?>
microtime(true) returns a floating point format, with microseconds after seconds + decimal point, and the accuracy is usually more stable. It is recommended to use this instead of gettimeofday() .
PHP 7.3 and later versions support hrtime() , which is a high-precision timer that returns nanosecond time, suitable for scenarios with extremely high requirements for time measurement accuracy.
<?php
// Returns the number of nanoseconds of the current time
$nanoseconds = hrtime(true);
echo "High resolution time in nanoseconds: " . $nanoseconds . "\n";
// Convert nanoseconds to seconds + Microseconds
$seconds = floor($nanoseconds / 1e9);
$microseconds = floor(($nanoseconds % 1e9) / 1e3);
echo "Seconds: $seconds, Microseconds: $microseconds\n";
?>
hrtime() does not represent system time, but a high-precision timer, suitable for performance testing and other needs.
If you need to format the output in time and microseconds are accurate, you can combine DateTime and DateTimeImmutable to handle it:
<?php
$date = DateTime::createFromFormat('U.u', microtime(true));
echo $date->format("Y-m-d H:i:s.u") . "\n";
?>
This outputs a time string containing microseconds, with an accurate format and readable.
gettimeofday() will have insufficient microsecond accuracy in some environments, and it is not recommended to rely on it for high-precision time measurements.
Use microtime(true) to obtain a timestamp with microseconds, which is simple and convenient, and has good compatibility.
If you need higher precision timing, it is recommended to use PHP 7.3+ hrtime() , which supports nanosecond-level timing.
DateTime object combined with microtime(true) can easily achieve output formatted time with microseconds.
These methods can be used in combination to solve the problem of microsecond inaccuracy in PHP and ensure that your application's time accuracy needs are met.
<?php
// example:use microtime(true) Get the current time and format the output
$microtime = microtime(true);
$date = DateTime::createFromFormat('U.u', $microtime);
echo "当前时间带Microseconds:" . $date->format("Y-m-d H:i:s.u") . "\n";
// use hrtime() Get high-precision time
$nanoseconds = hrtime(true);
$seconds = floor($nanoseconds / 1e9);
$microseconds = floor(($nanoseconds % 1e9) / 1e3);
echo "High-precision timer - Second: $seconds, Microseconds: $microseconds\n";
// Reference document address:https://gitbox.net/php/manual/en/function.microtime.php
?>