PHP/MySQL时区澄清

PHP/MySQL时区澄清,php,mysql,timezone,timestamp,Php,Mysql,Timezone,Timestamp,我现在有一个问题困扰着我的大脑,那就是根据用户的设置将我的MySQL时间转换成特定的时区 my MySQL的所有时间都以UTC时间存储,格式如下: 2009-11-0817:06:40 一旦我查询了时间,我不太确定如何使用PHP将其转换为适当的时区 因此,在上面的示例中,我想显示: 2009-11-08 09:06:40 以下是我目前拥有的(可能需要修复): 但是,它将我的时间(使用上面的示例)显示为: 2009-11-0815:06:40 有什么想法会导致这种情况吗?我可能错了,但它似乎在抱怨

我现在有一个问题困扰着我的大脑,那就是根据用户的设置将我的MySQL时间转换成特定的时区

my MySQL的所有时间都以UTC时间存储,格式如下:

2009-11-0817:06:40

一旦我查询了时间,我不太确定如何使用PHP将其转换为适当的时区

因此,在上面的示例中,我想显示:

2009-11-08 09:06:40

以下是我目前拥有的(可能需要修复):

但是,它将我的时间(使用上面的示例)显示为:

2009-11-0815:06:40


有什么想法会导致这种情况吗?

我可能错了,但它似乎在抱怨你的
echo$dt_obj语句。您可以尝试回显DateTime::format()的结果


编辑:对于你的新问题,我猜你的默认格式已经设置为PST,所以当创建新日期时,它是使用该时区创建的,因此设置时区不会改变任何内容。你可以检查一下,看看情况是否如此。您还可以查看
date\u default\u timezone\u set(“”)

我想这就是你想要的

$dt_obj = new DateTime($row['date']); 
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles')); 
echo $dt_obj->format('Y-m-d H:i:s');
使用

更新已编辑的问题:要确保PHP将数据库中的时间视为UTC时间,请执行以下操作:

$row['time'] = '2009-11-08 09:06:40';

$dt_obj = new DateTime($row['time'], new DateTimeZone('UTC')); 
echo 'UTC: ' . $dt_obj->format('Y-m-d H:i:s') . '<br />';  // UTC: 2009-11-08 09:06:40
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles')); 
echo 'Los Angeles: ' . $dt_obj->format('Y-m-d H:i:s');  // Los Angeles: 2009-11-08 01:06:40
$row['time']='2009-11-08 09:06:40';
$dt_obj=新日期时间($row['time',new DateTimeZone('UTC'));
回显“UTC:”$dt_obj->格式('Y-m-dh:i:s')。'
';//UTC:2009-11-08 09:06:40 $dt_obj->setTimezone(新日期时区(“美国/洛杉矶”); 回声“洛杉矶:”$dt_obj->格式('Y-m-dh:i:s');//洛杉矶:2009-11-08 01:06:40
服务器的默认时区是什么(在php.ini中;您可以通过default_timezone_get()或phpinfo()找到它)?看起来它设定的时区比美国/洛杉矶晚6小时)谢谢你的回复,GZipp。phpinfo()报告默认时区为:America/Chicago。就是这样。非常感谢。
$dt_obj = new DateTime($row['date']); 
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles')); 
echo $dt_obj->format('Y-m-d H:i:s');
$row['time'] = '2009-11-08 09:06:40';

$dt_obj = new DateTime($row['time'], new DateTimeZone('UTC')); 
echo 'UTC: ' . $dt_obj->format('Y-m-d H:i:s') . '<br />';  // UTC: 2009-11-08 09:06:40
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles')); 
echo 'Los Angeles: ' . $dt_obj->format('Y-m-d H:i:s');  // Los Angeles: 2009-11-08 01:06:40