How to change timezone in PHP website easily
Is your website out of time sync? Then you need to change timezone in PHP.
Luckily, PHP websites allow configuring date and time as per users’ choices.
However, this involves modifying server configuration which needs special care.
At Bobcares, we often get customer requests to set timezone as a part of our Server Management Services.
Today, we’ll see how we change timezone in PHP configuration.
How does PHP timezone affect websites?
Let’s begin by checking how the PHP timezone affects the working of a website.
PHP timezone is responsible for the date shown on the website. Moreover, it affects the scripts that run on the server. For instance, if there is a scheduled task running for the website, it runs at the specified time as per the timezone chosen.
Common reasons for setting PHP timezone
Usually, there can be many scenarios where website owners have to change the timezone in PHP. This includes:
- When the server is located in a different country and in a different time zone.
- By default, newer PHP versions default to UTC timezone
- A need to display a particular time on the website.
Steps to change timezone in PHP
Being in the hosting industry for more than a decade, our Support Engineers frequently receive requests to modify PHP timezone in the server.
The timezone can be set either using the php.ini file or via .htaccess file. Let’s now see how we edit it for our customers.
Set timezone using PHP.INI file
In the php.ini method, we edit the active PHP configuration file for the domain.
We then set the variable date.timezone to the desired value.
For instance, to change the timezone to Australian Melbourne Time for the PHP, we add the entry as:
date.timezone = "Australia/Melbourne"
For a custom time zone, it is always good to refer https://www.php.net/manual/en/timezones.php.
Finally, we add a PHP info page on the website to see that the new timezone is in effect.
PHP timezone is specific to each PHP version. Therefore, our Support Engineers make sure to edit the timezone in all PHP versions on the server.
Via .htaccess method
Similarly, when PHP runs as a DSO module, it requires editing the .htaccess file to set timezone. By default, to set the desired time on a website, we edit the main .htaccess file.
For instance, in cPanel servers, we edit the file at /home/username/public_html/.htaccess and add the following entry in it.
php_value date.timezone 'Australia/Melbourne'
Then we save it. Now, the PHP info page shows the value of the timezone correctly. Again, in this method, the value applies only to the specific folder of the domain.
[Looking for a way to change PHP settings on the server? We are available 24×7]
To sum up, to change timezone in PHP, we need to edit the php.ini file or .htaccess file. Today, we saw how our Support Engineers set up a custom time for websites and make it work.