gpt4 book ai didi

php - 在 PHP 中获取时间滴答

转载 作者:可可西里 更新时间:2023-10-31 22:42:10 24 4
gpt4 key购买 nike

在 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() 同样以秒和小数点后的微秒为单位返回时间,因此它具有更高的精度。由于一些古老的原因,默认值是一个字符串,但如果您将 true 作为第一个参数传递,您将得到一个漂亮的 float :

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

关于php - 在 PHP 中获取时间滴答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30817574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com