php中DateTimeZone 设置时区无效
怪我咯
怪我咯 2017-04-11 09:12:40
0
2
571
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

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(2)
大家讲道理

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

文档链接

Ty80
$dt = new DateTime('@'.$time);  
$tz = timezone_open('Asia/Shanghai');  
$dt->setTimezone($tz);  
echo $dt->format('Y-m-d H:i:s');//2017-01-12 13:59:08
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!