Changing the timezone with PHP
Posted December 31st, 2008 in 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 (whereas I have used the ini_set method described above recently for my Electric Bookmarks website to show Pacific Time dates and times when the web server actually uses NZ time). I'll try this function out at a later date and post about using that as well.
Related posts:
- PHP list of php.ini directives (Tuesday, December 30th 2008)
- Setting the MySQL timezone per connection (Saturday, December 20th 2008)
- Correcting the PHP timezone (Sunday, March 16th 2008)
- Formatting Dates with PHP (Thursday, December 20th 2007)
Subscribe / Follow / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email to have my posts sent in a daily email, follow me on Twitter or follow me on Facebook.
At least one new post is usually made every day. See my posting schedule for more details.
