欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP date()函数警告: It is not safe to rely on the system解决方法,saferely

程序员文章站 2022-05-07 15:19:57
...

PHP date()函数警告: It is not safe to rely on the system解决方法,saferely

近来总是有系统邮件提示,开始没在意,后来不断提示就看了一下。提示以下信息

复制代码 代码如下:


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 gett
ing this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Chongqing' for 'CST/8.0/no DST' instead in /data0/htdocs/www.qttc.net/function/function.php on line 542


大体是说timezone没有设置,在中国使用的是格林+8小时,所以需要设置一下。

第一种

在页面头部加入以下语句

复制代码 代码如下:


date_default_timezone_set("PRC");


这种方法有个缺点,就是所有的页面都得添加

第二种

在php.ini里找到date.timezone这行,把值改成PRC,如date.timezone = PRC。如果没有这一行直接加上就好。最后重启WEB服务器与PHP即可。

phpinfo()检查后提示的PHP错误提示

找到php.ini文件里面查找date.timezone
设置成这个就可以了date.timezone =Asia/Chongqing
 

我是用到date(Y-m-d H:i:s)函数的时,错误提示如下:

这只是一个警告(Warning),虽然可以忽略,但可能会发生执行结果错误。
你写的 date(Y-m-d H:i:s) 参数有问题,正确的写法是:
date('Y-m-d H:i:s',time()); //后面的,time()可以省略,但最好形成习惯写上,比较规范。

另外,如果你设置了时间区域(比如中国上海等),会更准确一些。
比如在文件的最前面这样写:
date_default_timezone_set('Asia/Shanghai'); //设置时间区域为“亚洲/上海”,也就是中国。
echo date('Y-m-d H:i:s',time());
?>

希望对你有帮助!