php时间戳有关问题

WBOY
Release: 2016-06-13 11:16:40
Original
872 people have browsed it

php时间戳问题

本帖最后由 hongming271 于 2013-03-05 16:20:16 编辑 php函数中的date函数在某些系统(如 Windows)中限制为从 1970 年 1 月 1 日到 2038 年 1 月 19 日。

现在我有个时间戳是超过了2038年后,用date不能正常转化成正确的标准时间格式。

还有一个问题就是我有个时间戳1356969600,转化成正常效果是2013-1-1 00:00:00
例子 
$dt = new DateTime([email&#160;protected]');<br />echo $dt->format('Y-m-d H:i:s');
Copy after login

通过例子转化却变成了2012-12-31 16:00:00,比正常效果足足少了八个小时


------解决方案--------------------

date_default_timezone_set("Europe/London");
$dt = new DateTime([email protected]');
echo $dt->getTimezone()->getName(),"\n";
echo $dt->format('Y-m-d H:i:s'), "\n";
echo date('Y-m-d H:i:s',1356969600),"\n";


date_default_timezone_set("Asia/Shanghai");
$dt = new DateTime([email protected]');
echo $dt->getTimezone()->getName(),"\n";
echo $dt->format('Y-m-d H:i:s'), "\n";
echo date('Y-m-d H:i:s',1356969600),"\n\n";


date_default_timezone_set("Asia/Shanghai");
$dt = new DateTime([email protected]');
echo $dt->getTimezone()->getName(),"\n";
echo $dt->format('Y-m-d H:i:s'), "\n";
echo date('Y-m-d H:i:s',1356969600),"\n";


$dt->setTimezone(new DateTimeZone('Asia/Shanghai'));
echo $dt->getTimezone()->getName(),"\n";
echo $dt->format('Y-m-d H:i:s'), "\n";
echo date('Y-m-d H:i:s',1356969600),"\n";

------解决方案--------------------
本帖最后由 xuzuning 于 2013-03-06 09:41:27 编辑

DateTime::format 被设计成不受外部设置影响(面向对象就应该如此)
所以无论你如何改变环境参数 date_default_timezone ,都不会影响输出结果

要想改变 DateTime::format 的时区设置,需要向
DateTime::setTimezone 传入一个时区对象 DateTimeZone

比如
$d = new DateTime([email&#160;protected]');<br />$d->setTimezone(new DateTimeZone('PRC'));<br />echo $d->format('Y-m-d H:i:s');<br />
Copy after login
2013-01-01 00:00:00 
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!