It is not safe to rely on the system’s timezone settings

在新配置的php环境中,写php程序中有时会出现这样的警告:

PHP Warning: 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 *.php on line * 。

这是因为 PHP 所取的时间是格林尼治标准时间,所以和你当地的时间会有出入,格林尼治标准时间和北京时间大概差8个小时左右,我们可以按照下面的方法解决:

  • 在页面头部使用 date_default_timezone_set() 设置默认时区为北京时间,即
date_default_timezone_set("PRC");
  • php.ini 中设置 date.timezone 的值为 PRC,设置好以后的为:
date.timezone=PRC
除非注明, 风波竹林 文章均为原创,转载请以链接形式标明本文地址
本文地址: https://blog.fbzl.org/it-is-not-safe-to-rely-on-the-systems-timezone-settings/