DateTime
不接受UNIX時間戳作爲其第一個構造函數參數。它反而期望像strtotime()
那樣的字符串。所以,你的代碼應該閱讀:
$objTimeZone = new DateTimezone("Europe/London");
// ...
$objDateTo = new DateTime("2011-05-04 19:30:00 - 24 hour");
$objDateTo->setTimezone($objTimeZone);
$date_to = $objDateTo->format('c');
要使用時間戳使用DateTime
你必須先用@
時間戳:
$ts = time();
$dt = new DateTime('@'.$ts);
順便說一句:你必須要小心的方式您正在創建DateTime
對象並應用時區。您正在創建的默認時區的DateTime
對象(通過php.ini
或通過date_default_timezone_set()
設置,那麼在應用Europe/London
時區到這個DateTime
對象實際上是:取的時間點2011-05-04 19:30:00 - 24 hour
在我的默認配置時區和然後我們想知道這個時間點是在Europe/London
時區的時間。有,如果你配置的時區沒什麼區別,其實是Europe/London
,但它可能是一個區別,如果該時區不同。
編輯(以舉個例子)
您的系統配置了默認時區UTC
。
$date1 = new DateTime(); // the null doesn't matter
// $date1 now contains approx. 2011-03-18T15:03:58+00:00
$date1->setTimezone(new DateTimezone('Europe/London');
// $date1 doesn't change because UTC is the same as Europe/London for the given date
$date1->setTimezone(new DateTimezone('Europe/Berlin');
// in Germany the same point in time is 2011-03-18T16:03:58+01:00
到目前爲止沒有問題。讓我們來看你的第二次約會。
$date2 = new DateTime("2011-05-04 19:30:00 - 24 hour");
// $date2 now contains 2011-05-03T19:30:00+00:00
$date2->setTimezone(new DateTimezone('Europe/London');
// $date2 now changes to 2011-05-03T20:30:00+01:00 because Europe/London moves to DST on March 27th
$date2->setTimezone(new DateTimezone('Europe/Berlin');
// that's 2011-05-03T21:30:00+02:00 in Germany
鋤頭,清理那一個。
你的語法錯了,用'format('c:');'代替!開玩笑:) – 2011-03-18 14:40:01