Forums  ›  Website and Blog Tricks  ›  php errors fix
 

Fix PHP date() Timezone Warnings

After I upgraded from PHP version 5.2 to 5.3, my websites exploded with PHP warning messages similar to this:

Warning: date() [function.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/Denver' for 'MST/-7.0/no DST' instead in...

 

This is a not a subtle warning and they pretty much trashed my sites.

  It turns out that someone decided that PHP 5.3 should start printing these errors if no default timezone is set.  So how do we get rid of them?

Well, there are two ways to fix this problem.  The first is to add a line to your php.ini (sometimes also named “php5.ini”) file as follows:

date.timezone="America/Los_Angeles"

 

More on the allowable timezone values in a bit.  This file is usually in the root of your server.  For the exact location, you can use the phpinfo() function to see your server settings.

The second way to fix the problem is to add this line to your PHP code before you use the date() function:

date_default_timezone_set('America/Los_Angeles');

 

Simple, right?  Now, on to the allowable timezone values.  For a list of allowable values (cities), check this link on the PHP site

A value of ‘UTC’ also works. 

That is the Universal Coordinated Time (yes, I realize the abbreviation doens’t match up, in English at least), which is the same as Grenwich Mean Time (UK basically).

  Oddly, standard time zone abbreviations like ‘EDT’ and ‘PDT’ do not work on my server, although they might work on yours. 

These are not officially supported so I suggest using the city names.

Which one is best?  I recommend using the second method in order to make your code completely portable. 

That way, if you switch your server or hosting provider, and the default timezone is not set in the php.ini file on that server, your code won’t explode with these errors again.

Hope this helps! – Brian