How to set the default timezone in PHP

Last updated on October 10, 2020 by Dan Nanni

Question: How can I set the default timezone used by all date/time related PHP functions?

The default timezone for PHP is defined in php.ini configuration file. So you will need to edit the file by hand.

First, find the location of php.ini on your system as follows. In this example, it is found at /etc/php5/cli/php.ini.

$ php --ini
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      /etc/php5/cli/conf.d/10-pdo.ini

Next, find out the timezone (TZ) string of your region. For that, you can use a command-line tool called tzselect. Simply run the following command:

$ tzselect

You will then go through interactive menu to identify your region.

After you are done, tzselect will print the value of TZ variable as follows. In this example, the TZ value is America/New York.

Now open up php.ini in a text editor, and look up date.timezone variable in it. If date.timezone variable definition is commented out, uncomment the line. Then change the timezone accordingly. You are done!

date.timezone = "America/New_York"

If you want to change the current timezone of your Linux system, refer to this tutorial.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean