Tag Archives: it is not safe to rely on the system timezone settings date.timezone date_default_timezone_set identifier

php版本大于5.3.0导致日期函数抛警告异常

这两天碰到因为php版本升级,调用一些函数产生警告信息,从而导致编码不正确。 php版本大于5.3.0的时候,使用date(),mktime()等和时区相关的函数可能会产生以下错误:

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.
Warning:  mktime(): 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.

产生该错误的原因是,php版本大于等于5.3.0之后,如果你的php没有设置默认的时区,使用时区相关函数,比如说date(),mktime()等函数就会产生这个警告。

1.解决办法:

1.1 设置变量date.timezone

在php.ini中设置变量date.timezone为一个有效的市区字符串,字符串值在这里有: http://php.net/manual/en/timezones.php 。例如:

date.timezone = "Asia/Jakarta";
date.timezone = "Asia/Shanghai";
date.timezone = "Europe/Berlin";

1.2 date_default_timezone_set()函数

在调用时区相关的函数之前,调用date_default_timezone_set()这个函数设置一个有效的时区字符串,有效的时区字符串在上面那个链接。例如:

date_default_timezone_set('America/Los_Angeles');
date_default_timezone_set('Asia/Shanghai');