简体   繁体   English

PHP strtotime在UTC时间返回false

[英]PHP strtotime returning false for UTC time

My colleague and I are obtaining different results from some unit tests that use strtotime. 我和我的同事从一些使用strtotime的单元测试中获得了不同的结果。

The discrepancy originates in this line: 差异源于以下这一行:

$value = strtotime('2050-05-01T20:10:29.410Z');

on my machine, this result returns the following: 在我的机器上,此结果返回以下内容:

int(2535048629)

whereas my colleague's version returns false 而我同事的版本返回false

We are both using PHP version 5.4.14 and PHPUnit 3.724. 我们都使用PHP 5.4.14和PHPUnit 3.724。

Has anyone got any idea what is causing this discrepancy, and is there a more robust approach? 有谁知道导致这种差异的原因,是否有更可靠的方法?

It's likely related to 32-bit vs 64-bit. 它可能与32位和64位有关。 Timestamps in the year 2050 are larger than 32 bits. 2050年的时间戳大于32位。

This is because he is on 32-bit and you are on 64-bit machine. 这是因为他使用的是32位,而您使用的是64位计算机。 See what echo PHP_INT_MAX; 看到什么echo PHP_INT_MAX; returns on both machines. 在两台机器上都返回。 More read here . 在这里阅读更多。

If you wish to get timestamp on 32-bit machine, you can use DateTime as: 如果希望在32位计算机上获取时间戳,可以将DateTime用作:

$value = new DateTime('2050-05-01T20:10:29.410Z');
echo $value->format('U');  // returns 2535048629 as string

or format inputed timestamp as: 或将输入的时间戳格式为:

$value = new DateTime('@2535048629');
echo $value->format('r'); // Sun, 01 May 2050 20:10:29 +0000

instead of date('r', '2535048629'); 而不是date('r', '2535048629'); which will not work on 32-bit machine. 在32位计算机上不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM