Home / Changing the timezone with PHP

Changing the timezone with PHP

Since PHP 5.1.0 it has been possible to change the default timezone that PHP uses using the date.timezone configuration, which can be set for the entire server in php.ini or httpd.conf, on a virtualhost by virtualhost basis, or using the ini_set() function. This post looks at how to change the timezone in PHP using the ini_set function.

Using ini_set() in your script to set the current timezone to e.g. the timezone that is used in Los Angeles, you would do this:

ini_set('date.timezone', 'America/Los_Angeles');

All subsequent requests to date and time functions would use America/Los Angeles as the timezone.

If you need to do this in your scripts, for example if the web server you are using is in a different timezone from the dates and times you wish to use in your scripts, then you should ensure the call is made at the very start of your scripts (or, if you can, move the setting out to an .htaccess file).

A complete list of the timezones available can be found here: http://www.php.net/manual/en/timezones.php

While looking at the various settings etc in the PHP manual I also came across the date_default_timezone_set function which I have not used personally myself. I’ll try this function out at a later date and post about using that as well.