Setting the Time Zone for PHP 5.3.0+

While working with PHP 5.3 scripts, you may have run into an error that tells you you need to set the time zone for PHP 5.3.0.  This might happen when upgrading or installing a CMS or other script in your hosted or local web environment. A comment error message I’ve run across several times when working with MODX, SilverStripe, Processwire, etc. is:

date.timezone option in php.ini must be set in PHP 5.3.0+

Another common error message is:

To use PHP 5.3.0+, you must set the date.timezone setting in your php.in.

In this brief post I’ll describe a couple of different ways to deal with this.

Background:

Prior to PHP 5.3 setting the time zone for PHP was not so critical. If one was not explicitly specified in the php.ini file, then the PHP engine would attempt to query the server it was running on and set the time zone based on that.  However, from PHP 5.3 on this method is no longer recommended, and therefore if your script is using any date-related features then the PHP engine requires you to explicitly set your time zone.

How to Set your Time Zone for PHP 5.3.0+

This is a pretty simple process.  I will describe two different approaches:

1. Set the Time Zone in php.ini

If you have access to your php.ini file, then this is the best and most direct approach to setting your time zone. Simply open your php.ini file for editing, and look for the line with the following:

date.timezone = {something here}

This is where you place your time zone.  For example I am located in Auckland, New Zealand, therefore my php.ini file in that line would read:

date.timezone =  Pacific/Auckland

Ensure that there’s no semicolon before this line, as this means that it’s commented out.  If there’s one, remove it.

If this line doesn’t exist anywhere in your php.ini file, add it somewhere.

Once you save the file, remember you need to restart Apache for the changes to take effect.

2.  Set the Time Zone in .htaccess

This method is a good alternative if you have no way to edit your php.ini file, or don’t want to.  You will however need to do this for every site you build/work on.

First, see if a .htaccess file exists in the document root folder of your website. If not then create one.   Remember the .htaccess file is a hidden file, so you’ll need to figure out how to show hidden files on your system.  Google is your friend 🙂

Then add the directive below to your .htaccess file:

php_value    date.timezone    {yourtimezone}

For example:

php_value    date.timezone    Pacific/Auckland

That’s pretty much it. Either one of these methods should work for setting the time zone that the PHP 5.3 engine will work with.

Supported Time Zones

To find the list of supported time zones, check out the relevant page on the PHP manual: http://us.php.net/manual/en/timezones.php, and click on your region for the list of time zones.

Hope this helps. If you have any other methods you use to set time zones, please feel free to leave a comment, or just drop a line to say hey 🙂

12 thoughts on “Setting the Time Zone for PHP 5.3.0+”

  1. I noticed this happening in Revo 2.2 where I couldn’t change the Template unless I entered the Time Zone in the System Settings. It was a PAIN to say the least, but seems to have been resolved.

    1. Ricardo, for a shared hosting server where you don’t have access to the php.ini file, use the second method, the one using the .htaccess file.

      mary

  2. hi, did you intend to use an open curly bracket and a close parenthesis?
    php_value date.timezone {your timezone)

Leave a Reply