2012-01-12 119 views
3

日期時間我有這樣的代碼:警告使用PHP

try{ 
    $datetime1 = new DateTime("now"); 
    $datetime2 = new DateTime("now"); 
    $interval = $datetime1->diff($datetime2); 
    var_dump($interval); 
} 
catch(Exception $e) { 
    echo $e->getMessage(); 
}  

我沒有看到任何結果關於var_dump,PHP的輸出是:

DateTime::__construct(): It is not safe to rely on the system's timezone setting 
s. You are *required* to use the date.timezone setting or the date_default_timez 
one_set() function. In case you used any of those methods and you are still gett 
ing this warning, you most likely misspelled the timezone identifier. We selecte 
d 'Europe/Paris' for '1.0/no DST' instead 

我使用php_cli 5.3。 8

我該怎麼辦? 感謝

回答

12

在PHP5.3,有必要設置時區。

打開php.ini,找到設置date.timezone。取消註釋並將其設置爲某個值,如

date.timezone = "Europe/Paris" 

然後重新啓動服務器。它應該照顧警告。

[編輯]

通過@greut建議將很好的工作解決方案。兩者之間的區別在於,php.ini設置是服務器範圍的(將被應用於現在和將來在服務器上運行的所有應用程序),而date_default_timezone_set()被應用於僅當前執行的應用程序。

php.ini解決方案可讓您忘記您運行的任何其他網站上的警告 - 但您需要訪問php.ini。使用date_default_timezone_set()將覆蓋php.ini中的設置,所以如果您願意,可以同時使用兩種方法。

+0

錯誤消息提示通過編程解決方案。加上一個用於暗示不涉及代碼修改的解決方案,這將導致更多的可移植代碼。 – davogotland 2012-01-12 12:43:35