使用 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() with 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');

Demo

易于阅读,易于维护。