如何以纳秒精度在 PHP 中获取时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4207343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:17:43  来源:igfitidea点击:

How to get time in PHP with nanosecond precision?

phpdatetimenanotime

提问by Matt

Is this even possible in PHP?

这在PHP中甚至可能吗?

If not, what is the highest precision available?

如果不是,可用的最高精度是多少?

回答by Jacob Relkin

The microtimefunction is what you're looking for.

microtime功能是你在找什么。

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

PHP 不提供精度高于微秒的函数。

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

system如果您运行的是 Linux,则可以使用该函数直接从机器获取值:

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

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

%s是秒的数量,附加%N,这是纳秒的数量。

回答by bcosca

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

microtime()是使用 PHP 词汇表的最高精度。如果需要纳秒精度,您还可以使用 Unix 进行系统调用date并指定%N格式。Unixntp_gettime()具有更高的分辨率并返回抖动/漂移/稳定性/移位/校准数据。

回答by Anatol Belski

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.

现在有了这个扩展,它的纳秒时间是可能的http://pecl.php.net/hrtime。然而,它是一个秒表类,用于在不同平台上适应尽可能高的分辨率。

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

从 PHP 7.3 开始hrtime(),核心http://php.net/manual/en/function.hrtime.php 中还有一个跨平台功能。