解決php的It is not safe to rely on the system's timezone settings.警告錯誤

解決php的It is not safe to rely on the system's timezone settings.警告錯誤

Warning: phpinfo() [function.phpinfo]: It is not safe to rely on the system's timezone settings. 

PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone2011-08-18 22:05   

在用PHP5.3以上的PHP版本時,只要是涉及時間的會報一個 

Warning: phpinfo() [function.phpinfo]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Chongqing' for 'CST/8.0/no DST' instead in /usr/local/apache/htdocs/index.php on line 2 

實際上,從 PHP 5.1.0 ,當對使用date()等函數時,如果timezone設置不正確,在每一次調用時間函數時,都會產生E_NOTICE 或者 E_WARNING 信息。而又在php5.1.0中,date.timezone這個選項,默認情況下是關閉的,無論用什麼php命令都是格林威治標準時間,但是PHP5.3中好像如果沒有設置也會強行拋出了這個錯誤的,解決此問題,只要本地化一下,就行了。 

以下是三種方法(任選一種都行): 

一、在頁頭使用date_default_timezone_set()設置 

date_default_timezone_set('PRC'); //東八時區 echo date('Y-m-d H:i:s');          

二、在頁頭使用 ini_set('date.timezone','Asia/Shanghai');

三、修改php.ini。打開php5.ini查找date.timezone 去掉前面的分號修改成爲:date.timezone ="PRC" PRC注意加上雙引號,要不還會出錯! 

重啓http服務(如apache2或iis等)即可。   

XXX可以任意正確的值。對於我們國內來說:可以爲以下值:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次爲重慶,上海,烏魯木齊)港臺地區可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次爲澳門,香港,臺北),還有新加坡:Asia/Singapore,當然PRC也行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章