PHP set timezone

PHP comes with a dedicated configuration setting for the timezone that it is supposed to use. On both Windows and Linux this setting usually comes blank and PHP defaults to using the system timezone when doing calculations using the date function for example.

On both Windows and Linux systems, the configuration to set the timezone is in the main php.ini file.

On Windows, the "php.ini" file is in the root of the folder which contains the PHP installation. On newer versions of Ubuntu Linux, this file is located at /etc/php5/apache2/php.ini. Open the file in your favourite editor; on Linux for example:

vi /etc/php5/apache2/php.ini

Find the section called [Date] which is about halfway through the configuration file. It is much easier to search for "date.timezone" as the file is pretty long.

Look for the line that says:

date.timezone =

And add your timezone after the equal sign.

For example, if you wanted to set the timezone to United States Eastern Time, you would change the line to this:

date.timezone = US/Eastern

You can also choose timezones in terms of how many hours they are from GMT. So, if you wanted to change the timezone to GMT+8, you would use the following format:

date.timezone = Etc/GMT+8

Save and close the configuration file and restart Apache. If you are running Linux, see how to restart Apache on Ubuntu or other Linux.

And that is how you set the timezone in PHP using the php.ini file. We hope you learnt a lot in this PHP tutorial.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...