如何在PHP中获取当前时间(以毫秒为单位)?

本文翻译自:How to get current time in milliseconds in PHP?

time()以秒为单位 - 是否有一个毫秒?


#1楼

参考:https://stackoom.com/question/FLHF/如何在PHP中获取当前时间-以毫秒为单位


#2楼

简短的回答是:

$milliseconds = round(microtime(true) * 1000);

#3楼

Use this: 用这个:

function get_millis(){ list($usec, $sec) = explode(' ', microtime()); return (int) ((int) $sec * 1000 + ((float) $usec * 1000)); }

Bye 再见


#4楼

Short answer: 简短回答:

64 bits platforms only! 仅限64位平台!

function milliseconds() {
    $mt = explode(' ', microtime());
    return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}

Long answer: 答案很长:

If you want an equilvalent function of time() in milliseconds first you have to consider that as time() returns the number of seconds elapsed since the "epoch time" (01/01/1970), the number of milliseconds since the "epoch time" is a big number and doesn't fit into a 32 bit integer. 如果你想要一个以毫秒为单位的time()的等价函数首先你必须考虑到,因为time()返回自“纪元时间”(01/01/1970)以来经过的秒数,自“纪元”以来的毫秒数time“是一个很大的数字,不适合32位整数。

The size of an integer in PHP can be 32 or 64 bit depending on platform. PHP中整数的大小可以是32位或64位,具体取决于平台。

From http://php.net/manual/en/language.types.integer.php 来自http://php.net/manual/en/language.types.integer.php

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 整数的大小取决于平台,尽管最大值约为20亿是通常的值(32位有符号)。 64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit. 64位平台的最大值通常约为9E18,Windows除外,它总是32位。 PHP does not support unsigned integers. PHP不支持无符号整数。 Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5. 整数大小可以使用常量PHP_INT_SIZE确定,最大值可以使用自PHP 4.4.0和PHP 5.0.5以来的常量PHP_INT_MAX。

If you have 64 bit integers then you may use the following function: 如果您有64位整数,那么您可以使用以下函数:

function milliseconds() {
    $mt = explode(' ', microtime());
    return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}

microtime() returns the number of seconds since the "epoch time" with precision up to microseconds with two numbers separated by space, like... microtime()返回自“纪元时间”以来的秒数,精度高达微秒,两个数字用空格分隔,如...

0.90441300 1409263371

The second number is the seconds (integer) preceeded by the decimal part. 第二个数字是小数部分之前的秒(整数)。

The function milliseconds take the integer part multiplied by 1000 函数milliseconds取整数部分乘以1000

1409263371000

and adds the decimal part multiplied by 1000 and rounded to 0 decimals 并将小数部分乘以1000并舍入为0小数

1409263371904

Note that both $mt[1] and the result of round are casted to int . 请注意, $mt[1]round的结果都被转换为int This is necessary because they are float s and the operation on them without casting would result in the function returning a float . 这是必要的,因为它们是float并且在没有强制转换的情况下对它们的操作将导致函数返回float

Finally, that function is slightly more precise than 最后,该功能稍微精确一些

round(microtime(true)*1000);

that with a ratio of 1:10 (approx.) returns 1 more millisecond than the correct result. 比率为1:10(大约)的值比正确结果多1毫秒。 This is due to the limited precision of the float type ( microtime(true) returns a float). 这是由于浮点类型的精度有限( microtime(true)返回浮点数)。 Anyway if you still prefer the shorter round(microtime(true)*1000); 无论如何你还是喜欢较短的round(microtime(true)*1000); I would suggest casting to int the result. 我建议将结果转换为int


Even if it's beyond the scope of the question it's worth mentioning that if your platform supports 64 bits integers then you can also get the current time in microseconds without incurring in overflow. 即使它超出了问题的范围,值得一提的是,如果您的平台支持64位整数,那么您也可以在几微秒内获得当前时间而不会产生溢出。

If fact 2^63 (approx. the biggest signed integer) divided by 10^6 * 3600 * 24 * 365 (approx. the microseconds in one year) gives approx. 如果事实2^63 (约为最大有符号整数)除以10^6 * 3600 * 24 * 365 (约为一年内的微秒)得到约。 292471 . 292471

That's the same value returned by echo PHP_INT_MAX / (1000000*3600*24*365); 这与echo PHP_INT_MAX / (1000000*3600*24*365);返回的值相同echo PHP_INT_MAX / (1000000*3600*24*365);

In other words a signed integer have room to store a timespan of over 200000 years measured in microseconds. 换句话说,有符号整数有空间存储超过200000年的时间跨度,以微秒为单位。

You may have then 那你可能有

function microseconds() {
    $mt = explode(' ', microtime());
    return ((int)$mt[1]) * 1000000 + ((int)round($mt[0] * 1000000));
}

#5楼

$timeparts = explode(" ",microtime());
$currenttime = bcadd(($timeparts[0]*1000),bcmul($timeparts[1],1000));
echo $currenttime;

NOTE: PHP5 is required for this function due to the improvements with microtime() and the bc math module is also required (as we're dealing with large numbers, you can check if you have the module in phpinfo). 注意:由于microtime()的改进,此功能需要PHP5,并且还需要bc数学模块(因为我们处理的是大数字,您可以检查是否在phpinfo中有模块)。

Hope this help you. 希望这对你有所帮助。


#6楼

$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
    ($the_date_time->format('u') / 1000);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值