使用php转换时区


Convert timezone using php?

如何使用php将时间戳2015-06-05 14:05:01转换为另一个时区?

我已经阅读并尝试了这里列出的许多方法,但我不能得到想要的结果。使用date_format($date,"M d h:i A")date_default_timezone_set('America/New_York'),我得到June 05 2:05 PM,这是服务器时区的原始起源,并正确。

我需要的是使用例如date_default_timezone_set('America/Los_Angeles')date_format($date,"M d h:i A")来转换2015-06-05 14:05:01以获得结果June 05 11:05 AM

使用DateTime()DateTimeZone():

// Create the DateTime() object and set the timezone to 'America/New_York'
$date = new DateTime('2015-06-05 14:05:01', new DateTimeZone('America/New_York'));
// Change the timezone to 'America/Los_Angeles'
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
// Print out the date and time in the new timezone
echo $date->format('M d h:i A');

演示

易于阅读,便于维护。