2012-11-13 33 views
1

我有一个PHP页面拉入日期字符串。如何使用PHP日期时区?

eg. 2012-11-10 11:37:06 

我知道这次是在一个特定的时区。

我想将此日期存储在UTC时间日期字段中,以便稍后可以将其转换为客户端的本地时间。

我该怎么做PHP

回答

3

要转换时区,使用DateTime类http://www.php.net/manual/en/class.datetime.php

// Create a DateTime object with your local time and local timezone. 
$d = new DateTime('2012-11-10 11:37:06', new DateTimeZone('Pacific/Auckland')); 

// Convert to UTC 
$d->setTimeZone(new DateTimeZone('UTC')); 

// outputs 2012-11-09 22:37:06 
echo $d->format('Y-m-d H:i:s'); 
+0

将在其自身的'$ D'适合插入'MySQL'日期字段?例如。 ''UPDATE Table1 SET Date1 = $ d“' – CJ7

+1

这是正确的,但您还需要确保您的MySQL连接时区设置为UTC,方法是发出以下命令:'SET time_zone ='+0:00';'在您之前做你的'UPDATE'命令。如果您确实知道您的MySQL机器的默认时区设置是UTC,您可以跳过此步骤。 –