php 32bit date parsing for dates before 13Dec 1901

 ̄綄美尐妖づ 提交于 2019-12-06 07:08:47

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5623647/php-32bit-date-parsing-for-dates-before-13dec-1901

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