Setting the Default Timezone for PHP Correctly

Posted: 2013-11-01 18:50:27

The default time-zone used and displayed by PHP is UTC time (Coordinated Universal Time), and this is why PHP’s time is always several hours off from your local time.

While it is usually best to leave the internal time-zone of PHP set on UTC (as it is a international time standard) and then convert (in PHP code) the UTC date-time value to the time-zone you’d like to display, to configure PHP to use a specific time-zone you have several options…

Set PHP’s Global TimeZone via php.ini

You can configure PHP to use your local time-zone by setting the proper value of “date.timezone” in php.ini.

Edit file:

Change this –

date.timezone = "UTC"

To this –

For West Cost:


For East Cost:


For Central Time:


Save file (and be careful not to change the filename extension from .ini to .ini.txt when doing so). Restart Apache for changes to take effect.

The big list of time zones for PHP is here –
For America
For the rest of the World

Setting PHP’s Per-Website TimeZone via VirtualHost

You also have the option of leaving PHP’s global default time-zone on UTC (in php.ini), and changing it per-website…

Edit the website’s HTTP and HTTPS VirtualHost files (select website in WampDeveloper’s Websites Tab, click the VirtualHost buttons to open the files). Then within the <VirtualHost> block, insert –

<IfModule php7_module>
php_admin_value date.timezone "America/New_York"
<IfModule php5_module>
php_admin_value date.timezone "America/New_York"

Or use directive “php_value” in the above line instead if you also want to allow the website’s .htaccess files and PHP scripts to further change that value at run-time.

Save the VirtualHost files. Restart Apache.

Note that this will only work if PHP is ran as an Apache module (mod_php), and not as a FCGI process (PHP-FCGI) – because the FCGI process is separate from the Apache process it can’t be configured by it (i.e., php settings can only be set via php.ini and cannot be set by VirtualHost nor .htaccess files).

Setting PHP’s Per-Directory TimeZone via .htaccess

For setting the time-zone per PHP script’s folder/directory…

Edit the location’s .htaccess file and add in the proper “php_value” setting (note that you can’t use “php_admin_value” in .htaccess files) –

<IfModule !fcgid_module>
php_value date.timezone "America/New_York"

Save the .htaccess file. There is no need to restart Apache after .htaccess edits.

This is the more portable way of setting the proper PHP values for your websites and scripts, but as mentioned previously, setting directives in .htaccess files only works under mod_php and does not work under PHP-FCGI.

Setting PHP’s Per-Script TimeZone via Code

Use the PHP function ini_set() to set your run-time values in script code…

ini_set("date.timezone", "America/New_York");

This is a good option if you are running PHP-FCGI, or are unable to make changes to php.ini, or can’t edit the website’s VirtualHost and .htaccess files.


It is important to always have a data.timezone value set. Otherwise, with this value undefined, PHP will:

1. Generate an error/warning for every time the date() and getdate() functions are called.

PHP Notice: in file /index.php on line x: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

2. Attempt to guess which timezone to use via OS settings, environment variables, and algorithms. In some cases this can negatively impact server performance by a factor of 2-5x (especially PHP 5.3 on Windows)!