0
這是很容易給定的GMT日期轉換爲本地時間,如果你給從這個名單在PHP中的時區標識符轉換GMT時間爲當地時間:http://www.php.net/manual/en/timezones.php使用時區偏移,而不是時區標識符
例如,你可以這樣做(其中$ fromTimeZone僅僅是「GMT」,$ toTimeZone是剛剛從列表(即「美國/芝加哥」)中的一個常量,而$日期時間是格林尼治標準時間爲準):
public static function convertToTimezone($datetime, $fromTimeZone, $toTimeZone, $format = 'Y-m-d H:i')
{
// Construct a new DateTime object from the given time, set in the original timezone
$convertedDateTime = new DateTime($datetime, timezone_open($fromTimeZone));
// Convert the published date to the new timezone
$convertedDateTime->setTimezone(timezone_open($toTimeZone));
// Return the udpated date in the format given
return $convertedDateTime->format($format);
}
但是,如果只是給定時區偏移量,我有問題將相同的GMT日期轉換爲本地時間。例如,我給出的不是「美國/芝加哥」,而是給予-0500(這是該時區的等價抵消)。
我試過的東西,如以下(其中$日期時間是我的GMT日期和$ toTimeZone是偏移量(在這種情況下,-0500)):
date($format, strtotime($datetime . ' ' . $toTimeZone))
我知道所有的日期()各種功能都基於服務器的時區。我似乎無法讓它忽略它,並使用明確給出的時區偏移量。
可能的[Javascript/PHP和時區]的副本(http://stackoverflow.com/questions/2319451/javascript-php-and-timezones) – joshholat 2012-04-20 16:36:07
您有數字偏移量。你爲什麼不加上或減去適當的小時數和分鐘數? – Celada 2012-04-20 17:56:27