当我们在新的服务器配置环境的时候很容易忽略掉时区设置(有些一键脚本在配置环境的时候默认帮我们配好了,像‘宝塔’、‘小皮’等),就很容易出现‘ It is not safe to rely on the system's timezone settings’错误,尤其是在记录日志的时候需要调用date()函数,
例如之前一个项目,使用的tp5.1,PHP版本是7.1,环境是nginx1.16,在基本使用中没有这个错误,但是在cli中就出现了如下错误:
PHP Fatal error: Uncaught exception 'think\exception\ErrorException' with message '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 the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in /home/web/gongyu.bm.sc.cn/thinkphp/library/think/log/driver/File.php:159
Stack trace:
#0 [internal function]: think\Error::appError(2, 'date(): It is n...', '/home/web/gongy...', 159, Array)
#1 /home/web/gongyu.bm.sc.cn/thinkphp/library/think/log/driver/File.php(159): date('Ym')
#2 /home/web/gongyu.bm.sc.cn/thinkphp/library/think/log/driver/File.php(58): think\log\driver\File->getMasterLogFile()
#3 /home/web/gongyu.bm.sc.cn/thinkphp/library/think/Log.php(256): think\log\driver\File->save(Array, false)
#4 /home/web/gongyu.bm.sc.cn/thinkphp/library/think/Log.php( in /home/web/gongyu.bm.sc.cn/thinkphp/library/think/log/driver/File.php on line 159
PHP Fatal error: Uncaught exception 'think\exception\ErrorException' with message '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 the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in /home/web/gongyu.bm.sc.cn/thinkphp/library/think/log/driver/File.php:159
Stack trace:
#0 [internal function]: think\Error::appError(2, 'date(): It is n...', '/home/web/gongy...', 159, Array)
#1 /home/web/gongyu.bm.sc.cn/thinkphp/library/think/log/driver/File.php(159): date('Ym')
#2 /home/web/gongyu.bm.sc.cn/thinkphp/library/think/log/driver/File.php(58): think\log\driver\File->getMasterLogFile()
关键内容:
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 the timezone 'UTC' for now, but please set date.timezone to select your timezone
抛出的异常很明显的告诉我们时区错误,tp在写入日志的时候调用date()函数被php捕获到时区不安全,然后抛出了一个 fatal error
解决办法:设置当前区域的时区
1.在php.ini里加上找到date.timezone项,设置 date.timezone = "Asia/Shanghai",然后重载配置,
2.在每个方法前加上 ini_set('date.timezone','Asia/Shanghai') 或者使用 date_default_timezone_set("PRC");