php 获取当前时间的错误
之前一直用这个方法获取当前时间:
1 | $now = new DateTime(); |
不过貌似在「PHP 5.3.3」中,会报这样的错误
PHP Fatal error: Uncaught exception
'Exception'
with message'DateTime::__construct()
: 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 **
于是按照要求,执行
1 | date_default_timezone_set('Asia/Chongqing'); |
即可。
经过搜索,也可以修改php.ini
文件,找到「date.timezone」,修改一下
1 | date.timezone = Asia/Chongqing |
然后重启apache即可