在 PHP 中获取时间滴答声

Get Time Ticks in PHP

考虑 C# 中的这行代码

ordernumber.Value = DateTime.Now.Ticks.ToString();

如何在 PHP

中获得相同的 ordernumber.Value
$ordernumberValue = microtime(); //?

我试试这个

echo microtime(true) * 10000000;

但是得到的结果string.length是不同的。 比 C# 短。

来自 .NET 文档:

DateTime.Ticks Property

The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 (0:00:00 UTC on January 1, 0001, in the Gregorian calendar), which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.

在 PHP 中,这被简单地实现为 time():

time

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

microtime()类似returns时间以秒和小数点后微秒为单位,所以精度更高。由于某些古老的原因,默认值是一个字符串,但是如果您将 true 作为第一个参数传递,您将得到一个漂亮的浮点数:

rr-@burza:~$ php -r 'echo microtime(true);'
1434193280.3929%    

所以您所要做的就是将 time()microtime() 返回的值按常数因子缩放。

根据维基百科,一纳秒等于 1000 皮秒或 1⁄1000 微秒,或 1/1000000000 秒。所以 100 纳秒意味着 100/1000000000 微秒,即一个 .NET tick = 1/10000000 秒,即一秒 = 10000000 .NET tick。因此,您需要将 time()microtime() 返回的值乘以 10000000,如下所示:

microtime(true) * 10000000

我不确定这是否是您要查找的内容:-

$mt = microtime(true);

$mt =  $mt*1000; //microsecs
$ticks = (string)$mt*10; //100 Nanosecs
echo $ticks; //14341946614384

现在的主要区别是 Ticks 自 0001 年 1 月 1 日午夜 12:00:00 以来为 100 纳秒,而自 1970 年 1 月 1 日以来将产生 100 纳秒

一个刻度是秒的 1/10000000。

此代码将当前微时间转换为 "ticks" 计数:

list($usec, $sec) = explode(" ", microtime());
$ticks = (int)($sec*10000000+$usec*10000000);