Setting the Default Timezone for PHP Correctly

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)!

3 thoughts on “Setting the Default Timezone for PHP Correctly”

  1. Test code…

    echo "Today is" . date("d-M-Y"). "";
    echo "Today is " . date("l") . "";
    echo "The time is " . date("h:i:sa");
  2. Here is a good reason to keep UTC…

    And some discussion here…

    I don’t think the article points this out, but another strong point in favor of using UTC (or anyways some timezone that (by _definition_) doesn’t have DST) is that then you won’t have discontinuities in your monitoring timeseries. It’s just lovely when you have two datapoints for “cpu usage at 1:30am”, similarly it’s just lovely when your alerts fire because there’s a 1h gap in your timeseries when the clocks jump forward.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>