Last updated on July 11th, 2024 at 05:11 am
Every Apache/PHP system works on a timezone. The default timezone for Apache/PHP is UTC (Coordinated Universal Time). Sometimes you may need to change Apache/PHP time zone to sync it with other systems or because your customers are located in a different timezone. In this article, we will look at how to change default timezone in Apache/PHP. You can use these steps to set Apache/PHP timezone as per your requirement.
Why Change Default Timezone in Apache
The server timezone is one of the most important parameter in every website and app. It dictates the date and time for almost every thing that happens in your website or app. For example, it decides the datetime values of critical things like user registration, login, sign out, payment, time of browsing, and more. If this is not correct, then your server logs and data will be unreliable.
Also, if it is not as per your requirement, then you will need to convert the datetime values to a different time zone before analyzing it. For example, if you run a local website based out of San Francisco and all your users are located nearby and if your server stores all datetime values in UTC time zone, then you will need to unnecessarily convert all your date to PDT/PST time zone every single time. Therefore, it is essential to set your server’s date time to the appropriate value at the time of installation, if not later.
Change Default Timezone in Apache/PHP
Here is how to set Apache/PHP timezone. We will look at two ways to set default timezone – using .htaccess in Apache and using php.ini file in PHP.
Change default timezone using .htaccess file
In case you don’t use Apache with PHP, or you don’t have access to php.ini, you can set default time in Apache web server using .htaccess file. Before proceeding further, please make sure that you have enabled mod_rewrite (.htaccess) on your Apache web server.
1. Open .htaccess file
Open .htaccess file, typically located at /var/www/html/.htaccess
$ sudo vi /var/www/html/.htaccess
2. Set TZ Variable
Apache supports numerous environment variables that control the server execution environment. Among them, TZ environment variable contains the time zone information. You can set or reset environment variables using SetEnv directive. Add the following line to set environment variable TZ (for time zone) in Apache web server to Eastern Time zone in US.
SetEnv TZ America/New_York
Save and close the file.
In the above line, we specify SetEnv TZ to set time zone environment variable in Apache, followed by ‘America/New York’ to specify Eastern Time. Here is a list of all available time zones in Apache/PHP.
3. Restart Apache Server
Restart Apache Server to apply changes.
$ sudo service apache2 restart
Change default timezone using php.ini
If you don’t have access to .htaccess file or if you use PHP with Apache, then here is how to set default time zone in Apache/PHP.
1. Open php.ini
Open php.ini, typically at /etc/php.ini. If you cannot find it then use the ‘locate php.ini’ command.
$ sudo vi /etc/php.ini
2. Change Time Zone
PHP.ini is the main configuration file for PHP. It contains many variables that control the PHP execution environment. Among them, date.timezone stores the value of time zone used by PHP installation. It is commented by default. Uncomment/Add the following lines to set default time zone in PHP.
;[Date]
;date.timezone = UTC
Change them to the following, to set default time to Eastern timezone.
[Date]
date.timezone =
America/New_York
Save and close the file.
3. Restart Apache Server
Restart Apache Server to apply changes.
$ sudo service apache2 restart
Conclusion
That’s it. Now your default time zone in Apache will be changed. In this article, we have learnt 2 different ways to set the time zone in Apache/PHP systems. You can change it either in Apache or PHP. If you want to change it in Apache, then you can do so by modifying .htaccess file. If you want to change it in PHP then modify php.ini file. Between the two solutions, it is advisable to change .htaccess file since PHP is usually handled by programmers and server settings are handled by administrators, and setting server time zone is done by site admins.
As mentioned earlier, it is very important that your server shows the correct date time values otherwise all its actions will be recorded with wrong timestamp. Please note, if you change the server time zone, it will not change any of the past values. It will only start recording future events with the new time zone. So it is advisable to do this at the time of installation itself, if not later.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Also read
How to Set Up Apache Virtual Hosts in Apache
How to Create Custom 404 Page in Apache
How to Install mod_security in CentOS 7
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.