I just updated one of my CentOS systems to PHP 5.3 (a chore best reserved for another blog post), and started to see the following warning popping up:
PHP Notice: in file index.php on line 15: 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 ‘America/Chicago’ for ‘CDT/-5.0/DST’ instead
Annoying to say the least. The fix is much easier than the message makes it seem.
PHP 5.3 now requires that you either have a timezone set in your php.ini file or that you pass the desired timezone via the
date_default_timezone_set() function before calling the
I opened my server’s
/etc/php.ini file and searched for timezone. My ini file had a section like the following:
[Date] ; Defines the default timezone used by the date functions ; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone ; date.timezone =
I uncommented the
date.timezone line and added the timezone I wanted.
[Date] ; Defines the default timezone used by the date functions ; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone date.timezone = 'America/Chicago'
There are two very important things to do in order for this to work properly for you:
- Use a timezone that is appropriate for your needs. Use PHP’s List of Supported Timezones to find the timezone that works for you.
- Since your server is likely to cache the PHP configuration, you will want to restart your web server process in order for the change to be recognized. The command to execute varies by system, but for most systems, the following will work:
[user@server /etc]$ sudo service httpd restart [sudo] password for user: Stopping httpd: [ OK ] Starting httpd: [ OK ] [user@server /etc]$
Of course, if you are already root, you won’t need to use
[root@server /etc]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ] [root@server /etc]#