Setting the default Timezone for PHP to use

The default time-zone used and displayed by PHP is UTC (Coordinated Universal Time)… Hence why it might look several hours off from your *local* time.

While sometimes it’s 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 whichever time-zone you’d like to utilize and display…

To set your system’s or server’s PHP to use a specific time-zone…

Setting PHP’s Global TimeZone via php.ini

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

Edit file:
C:\WampDeveloper\Config\Php\php.ini

Change this -

date.timezone = "UTC"

To this -

For West Cost:

date.timezone="America/Los_Angeles"

For East Cost:

date.timezone="America/New_York"

For Central Time:

date.timezone="America/Chicago"

Save file (and be careful not to change it’s extension when doing so). Restart Apache.

The 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 on UTC (in php.ini), but 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 those files). Then within the <VirtualHost> block, insert -

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

Or use directive “php_value” in the above line instead – if you also want to allow .htaccess files and PHP scripts in that website to be able 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 that process is separate from Apache and can’t be configured by it.

Setting PHP’s Per-Directory TimeZone via .htaccess

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

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

<IfModule php5_module>
php_value date.timezone "America/New_York"
</IfModule>

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

In some cases, the time-zone might already be set there already.

This is the more portable way of setting the proper PHP values for your websites and scripts, but as mentioned above, those directives only work for mod_php and do not work for PHP-FCGI.

Setting PHP’s Per-Script TimeZone via Code

Use the PHP function ini_set() to set your runtime 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.

date.timezone

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

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=""> <strike> <strong>