How to get time in PHP with nanosecond precision?

故事扮演 提交于 2019-11-30 08:09:19

The microtime function is what you're looking for.

PHP does not supply a function that has higher precision than microseconds.

You can use the system function to get the value straight from the machine if you are running Linux:

$nanotime = system('date +%s%N');

%s is the amount of seconds, appended by %N, which is the amount of nanoseconds.

microtime() is the highest precision using PHP's vocabulary. You can also make a system call by using Unix date and specify %N format if you need nanosecond accuracy. Unix ntp_gettime() has even higher resolution and returns jitter/wander/stability/shift/calibration data.

Now with this extension it's nanosecond timing is possible http://pecl.php.net/hrtime . Yet it is a stopwatch class implemented to suite the highest possible resolution on different platforms.

As of PHP 7.3 there's also a crossplatform hrtime() function in the core http://php.net/manual/en/function.hrtime.php.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!