Changing the timezone with PHPChanging 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:

Share or Bookmark

Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.

Subscribe or Follow

Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

Comments

blog comments powered by Disqus