一个关于 date() 函数的安全警告

我并不常看服务器上的 PHP 错误日志。今天发现一个关于 date() 函数的安全警告经常出现。原来是因为我没有在 PHP 的配置文件 php.ini 中设置默认的时区。

在服务器 PHP 的 error.log 中发现的错误提示如下:

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 the timezone ‘UTC’ for now, but please set date.timezone to select your timezone.

大意是说,依靠系统的时区来决定 data() 的输出是不安全的,而应该在 php.ini 中配置指定的时区。

好在解决方法很简单:编辑 php.ini 文件,找到 date.timezone = ,(默认为注释掉的状态,前面有个 ; 号),在它下面新建一行:

date.timezone = "Asia/Hong_Kong"

然后重起 Apache 服务,

service httpd restart

就可以了。

嗯,我用的 +8 时区,比较喜欢选香港。需要注意的就是,PHP 里面对时区的命名与 Linux 系统,比如我这个 VPS 上的基于 CentOS 的 LAMP 系统,命名稍有区别。

 ©

本文发表于水景一页。永久链接:<http://cnzhx.net/blog/php-warning-on-date-function/>。转载请保留此信息及相应链接。

时间过去太久,评论已关闭。
如果您有话要说,请到讨论区留言并给出此文章链接。
谢谢您的理解 :-)