一聚教程网:一个值得你收藏的教程网站

热门教程

PHP中date函数date(): It is not safe to rely

时间:2022-06-24 19:54:09 编辑:袖梨 来源:一聚教程网


在PHP代码中调用date函数时,在日志里看到如下报错

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 'Asia/Chongqing' for 'CST/8.0/no DST' instead

为什么会出现这样的报错?

从 PHP 5.1.0 引用了时区设置(date.timezone),但其默认又是关闭的,所以使用date()等函数时,都会产生E_NOTICE 或者 E_WARNING 信息。

处理方法1

在调用date前加入下代码


在页头加入代码:ate_default_timezone_set("PRC");
在页头加入代码:ini_set('date.timezone','注释:RPC');
在php.ini中启用date.timezone设置并设置其值:date.timezone=PRC,并重启apache;

注释:RPC代表中华人民共和国。

热门栏目