php 以毫秒为单位获取当前时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31223371/
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
Get current timestamp with milliseconds
提问by Vishnu
I want to get current timestamp with milliseconds in PHP something like below, in JavaScript I use Date.now()
我想在 PHP 中以毫秒为单位获取当前时间戳,如下所示,在我使用的 JavaScript 中 Date.now()
1436030635348
I tried something like below:
我试过如下:
$time = str_replace(".","",microtime(true));
But sometime it is not working properly. Like it prints one or two digits less.
但有时它不能正常工作。就像它少打印一两位数字一样。
回答by GolezTrol
It can print less digits, because it is a float value. So if you get 1234.0005
, that `.0005 actually means 500 microseconds. The zeroes after the 5 are lost because it's still an unformatted float value.
它可以打印更少的数字,因为它是一个浮点值。所以如果你得到1234.0005
,那个 `.0005 实际上意味着 500 微秒。5 之后的零丢失,因为它仍然是未格式化的浮点值。
The function microtime
is based on the system call gettimeofday()
, which also isn't accurate to the microsecond. Commonly it's accurate to 10 microseconds. See also Linux - Is gettimeofday() guaranteed to be of microsecond resolution.
该函数microtime
基于系统调用gettimeofday()
,它也不是精确到微秒。通常它精确到 10 微秒。另请参阅Linux - gettimeofday() 是否保证具有微秒分辨率。
-edit- I see the specs in your question have changed from microseconds to milliseconds.
-edit- 我看到您问题中的规格已从微秒变为毫秒。
To get the float value you have as an integer, you can multiply by a value. The float value represents seconds. Multiply by 1000 to get milliseconds or 1,000,000 to get microseconds. Since the specs (now) say milliseconds, you should multiply by 1000. 10k will give you accuracy of 1/10ms = 100μs. A millisecond is one thousandth of a second. A microsecond is one millionth of a seconds.
要获得作为整数的浮点值,您可以乘以一个值。浮点值表示秒。乘以 1000 得到毫秒或 1,000,000 得到微秒。由于规格(现在)说的是毫秒,您应该乘以 1000。10k 将为您提供 1/10ms = 100μs 的准确度。一毫秒是千分之一秒。一微秒是百万分之一秒。
Long story short, to get the time in integer milliseconds, use this:
长话短说,要以整数毫秒为单位获取时间,请使用以下命令:
$milliseconds = intval(microtime(true) * 1000);
Note: there is a reason why you get the time as a string or a float by default. The reason is that on 32 bit systems, PHP's integer is also 32 bits and is not large enough to contain the timestamp including milliseconds and microseconds. So this solution will only work well on a 64 bit system.
注意:默认情况下您将时间作为字符串或浮点数获取是有原因的。原因是在 32 位系统上,PHP 的整数也是 32 位,并不足以包含包括毫秒和微秒在内的时间戳。所以这个解决方案只适用于 64 位系统。
回答by Valinurov Alexander
$timeStampData = microtime();
list($msec, $sec) = explode(' ', $timeStampData);
$msec = round($msec * 1000);
$result = $sec . $msec;
Something like this
像这样的东西
But note, that js date.now() return time in MILLIseconds, not MICROseconds
但请注意,js date.now() 以毫秒为单位返回时间,而不是 MICROseconds
回答by Randika Vishman
I think you just need to use this function to get a Timestamp with micro seconds in PHP:
我认为您只需要使用此函数即可在 PHP 中获取微秒的时间戳:
int microtime ( void )
Use it as shown in the following link, following code snippet shows you how I'd altered it according to be helpful to your question:
如以下链接所示使用它,以下代码片段向您展示了我如何根据对您的问题有帮助的方式更改它:
<?php
function microtime_microseconds()
{
list($usec, $sec) = explode(" ", microtime());
return round(($usec * 1000) + $sec);
}
$micro_time = microtime_microseconds();
echo $micro_time;
?>
And also for reference regarding to that microtime()
PHP function, please visit and read this PHP manual page.
关于该microtime()
PHP 函数的参考,请访问并阅读此PHP 手册页。