This is a discussion on Setting the default time zone in PHP within the Programming forums, part of the Tutorials category; Setting the default time zone in PHP As its name indicates, the World Wide Web is international, and one of ...
Setting the default time zone in PHP
As its name indicates, the World Wide Web is international, and one of the biggest frustrations of handling dates in PHP scripts arises when your server is in a different time zone from your target audience. Since PHP 5.1, this is no longer a problem. The server administrator should set the default time zone in php.ini, using the date.timezone directive. You can check the value on your server by running phpinfo() and scrolling down to the date section. As you can see from Figure 1, Default timezone on my server is set to Europe/London.
Figure 1: It is a good idea to check the time zone being used by your server.
There are far too many time zones to list here, but you can find a list of all time zones supported by PHP at PHP: List of Supported Timezones - Manual. If the default time zone on your server doesnít suit your needs, never fear. There are several ways to reset it. If the server is under your own control, the best way to do it is to change the value of date.timezone in php.ini, and restart your web server. If you are on shared hosting and donít have access to php.ini, you can choose from one of the following methods (replace Europe/London in the following examples with the appropriate time zone from among those listed at the previous URL).
Resetting the time zone in .htaccess
If your server runs on Apache, and your hosting company has set the correct permissions for you to use an .htaccess file, add the following command to the .htaccess file in your site root:
php_value date.timezone 'Europe/London'
This changes the default time zone for every page on the site.
Resetting the time zone in individual scripts
If you canít change the configuration for the whole site, or need to change the time zone only for a specific script, add the following line of code before using any date functions:
Alternatively, use the following code:
Both do exactly the same. It doesnít matter which you use. It is a good idea to put configuration changes like this at the top of the script so that they are immediately obvious and available.