今天發(fā)現(xiàn)在php5.4使用date函數(shù)時(shí)提示PHP Warning: date() [function.date]:It is not safe to rely on the system’s timezone錯誤,但在php5.2中沒有任何問題,那是怎么一回事呢?
在用PHP5.3以上的PHP版本時(shí),只要是涉及時(shí)間的會報(bào)一個
“PHP Warning: date() [function.date]: 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 ‘UTC’ for ’8.0/no DST’ instead in”
這樣的錯,如何解決呢,實(shí)際上,從 PHP 5.1.0,當(dāng)對使用date()等函數(shù)時(shí),如果timezone設(shè)置不正確,在每一次調(diào)用時(shí)間函數(shù)時(shí),都會產(chǎn)生E_NOTICE 或者 E_WARNING 信息,而又在php5.1.0中,date.timezone這個選項(xiàng),默認(rèn)情況下是關(guān)閉的,無論用什么php命令都是格林威治標(biāo)準(zhǔn)時(shí)間,但是PHP5.3中好像如果沒有設(shè)置也會強(qiáng)行拋出了這個錯誤的,解決此問題,只要本地化一下,就行了.
以下是三種方法(任選一種都行):
一、在頁頭使用date_default_timezone_set()設(shè)置
date_default_timezone_set(‘PRC’); //東八時(shí)區(qū) echo
date(‘Y-m-d H:i:s’);
二、在頁頭使用 ini_set(‘date.timezone’,'Asia/Shanghai’);
三、修改php.ini,打開php5.ini查找date.timezone 去掉前面的分號修改成為:date.timezone =PRC
重啟http服務(wù)(如apache2或iis等)即可,XXX可以任意正確的值,對于我們國內(nèi)來說,可以為以下值,Asia/Chongqing,Asia/Shanghai,Asia/Urumqi.依次為重慶,上海,烏魯木齊,港臺地區(qū)可用:Asia/Macao,Asia/Hong_Kong,Asia/Taipei,依次為澳門,香港,臺北.還有新加坡:Asia/Singapore,當(dāng)然PRC也行。
總結(jié):原來在php5.3之前的版本對日期沒有嚴(yán)格的限制所以就是我日期設(shè)置不正確也沒有問題,但在php5.3之后就不行了.
新聞熱點(diǎn)
疑難解答