PHP strtotime returning false for UTC time

家住魔仙堡 提交于 2019-12-09 03:58:43

问题


My colleague and I are obtaining different results from some unit tests that use 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

We are both using PHP version 5.4.14 and PHPUnit 3.724.

Has anyone got any idea what is causing this discrepancy, and is there a more robust approach?


回答1:


This is because he is on 32-bit and you are on 64-bit machine. See what 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:

$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'); which will not work on 32-bit machine.




回答2:


It's likely related to 32-bit vs 64-bit. Timestamps in the year 2050 are larger than 32 bits.



来源:https://stackoverflow.com/questions/19547043/php-strtotime-returning-false-for-utc-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!