php 32位日期解析日期在13Dec 1901之前 [英] php 32bit date parsing for dates before 13Dec 1901

查看:186
本文介绍了php 32位日期解析日期在13Dec 1901之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很开心在我的开发机器上使用 strtotime()解析日期中的一些是在十九世纪,在十五世纪是极端的例子。



但是我的开发机器是64位,服务器是32位。在32位机器之前的任何日期前五月13日,1901年12月13日20:45:54 UTC超出范围(见strtotime注)。



我很高兴写一些这样的自定义代码,但是以为我会问是否有任何php内置函数会管理这个。日期为ISO 8601格式。



对于什么是值得的服务器是centos 5.4与php 5.2.10

解决方案


1901-12-12T05:00:00Z例如


我不能现在测试它,但 DateTime 的构造函数 可以可以接受您的格式,而不需要 createFromFormat 。我无法从文档中真正地告诉我们。



尝试

  try {
$ date = new DateTime('1901-12-12T05:00:00Z');
} catch(异常$ e){
echoArggh!$ e-> getMessage();
die();
}

echo $ date-> format('Y-m-d H:i:s');

,看看会发生什么,特别注意是否了解 Z 正确(即使它成为UTC时间)。



DateTime 在任何系统内部使用64位数字,并且没有范围限制。



如上所述,从5.3开始,您将可以使用 createFromFormat ,可以解析由 date()的占位符之一表达的任何日期。这是最佳的方式,因为你迫使它解析一个特定的模式,而不是猜测它。


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.

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).

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.

For what it is worth the server is centos 5.4 with php 5.2.10

解决方案

1901-12-12T05:00:00Z for example

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

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');

and see what happens, paying special attention to whether it understands the Z properly (i.e. makes it a UTC time).

DateTime uses 64 bit numbers internally on any system, and has no range limitations.

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位日期解析日期在13Dec 1901之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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