问题描述
我很高兴在我的开发机器上使用 strtotime()解析日期,其中一些在 1800 年代,在 1500 年代有一个极端的例子.
I was happily using strtotime() on my development machine to parse dates some of which are in the 1800s, with an extreme example in the 1500s.
但是我的开发机器是 64 位的,而服务器是 32 位的.在 32 位机器上,UTC 时间 1901 年 12 月 13 日星期五 20:45:54 之前的任何日期都超出范围(参见 strtotime 注释).
But my development machine is 64 bit and the server is 32 bit. On a 32 bit machine any date before Fri, 13 Dec 1901 20:45:54 UTC is out of range (see notes on strtotime).
我很高兴为此编写一些自定义代码,但我想我会问是否有任何 php 内置函数可以管理它.日期采用 ISO 8601 格式.
I am happy to write some custom code for this, but thought I would ask if any of the php inbuilt functions will manage this. The dates are in ISO 8601 format.
值得的服务器是带有 php 5.2.10 的 centos 5.4
For what it is worth the server is centos 5.4 with php 5.2.10
推荐答案
我现在无法测试,但是 DateTime
的构造函数 可能 无需createFromFormat
即可接受您的格式.我真的无法从文档中看出.
I can't test it right now, but DateTime
's constructor may be able to accept your format without needing createFromFormat
. I can't really tell from the documentation.
试试
try {
$date = new DateTime('1901-12-12T05:00:00Z');
} catch (Exception $e) {
echo "Arggh! ".$e->getMessage();
die();
}
echo $date->format('Y-m-d H:i:s');
看看会发生什么,特别注意它是否正确理解 Z
(即使其成为 UTC 时间).
and see what happens, paying special attention to whether it understands the Z
properly (i.e. makes it a UTC time).
DateTime
在任何系统内部都使用 64 位数字,并且没有范围限制.
DateTime
uses 64 bit numbers internally on any system, and has no range limitations.
如前所述,从 5.3 开始,您将能够使用 createFromFormat
可以解析由 date()
的占位符之一表示的任何日期.这是最佳方式,因为您强制它解析特定模式,而不是猜测它.
As said, from 5.3 on, you will be able to use createFromFormat
which can parse any date expressed by one of date()
's placeholders. This is the optimal way, because you force it to parse a specific pattern, instead of guessing it.
这篇关于php 32 位日期解析 1901 年 12 月 13 日之前的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!