PHP strtotime为UTC时间返回false [英] PHP strtotime returning false for UTC time

查看:38
本文介绍了PHP strtotime为UTC时间返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的同事从一些使用 strtotime 的单元测试中获得了不同的结果.

My colleague and I are obtaining different results from some unit tests that use strtotime.

差异源于这一行:

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

在我的机器上,此结果返回以下内容:

on my machine, this result returns the following:

int(2535048629)

而我同事的版本返回 false

我们都使用 PHP 5.4.14 版和 PHPUnit 3.724.

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?

推荐答案

这是因为他在 32 位机器上而你在 64 位机器上.看看 echo PHP_INT_MAX; 在两台机器上返回什么.更多阅读此处.

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.

如果你想在 32 位机器上获取时间戳,你可以使用 DateTime 作为:

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

而不是 date('r', '2535048629'); 这在 32 位机器上不起作用.

instead of date('r', '2535048629'); which will not work on 32-bit machine.

这篇关于PHP strtotime为UTC时间返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆