php中DateTimeZone 设置时区无效
本文介绍了php中DateTimeZone 设置时区无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
date_default_timezone_set('asia/shanghai');
//echo date_default_timezone_get();
//echo date('Y-m-d H:i:s',time());
echo date_default_timezone_get(); // asia/shanghai
$time = time();
$timezone = new DateTimeZone('Asia/Shanghai');
$date = new DateTime('@'.$time, $timezone);
var_dump($timezone->getLocation());// Beijing Time
echo $date->format('Y-m-d H:i:s');// 2017-01-11 12:32:20
echo '<br />';
echo date('Y-m-d H:i:s',$time);// 2017-01-11 20:32:20
输出:
asia/shanghai
array(4) {
["country_code"]=>
string(2) "CN"
["latitude"]=>
float(31.23333)
["longitude"]=>
float(121.46666)
["comments"]=>
string(12) "Beijing Time"
}
2017-01-11 12:32:20
2017-01-11 20:32:20
解决方案
Note: The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
这篇关于php中DateTimeZone 设置时区无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文