Setup PHP Timezone in Apache on Ubuntu 24.04

This article details how to set the correct system timezone in PHP with Apache on Ubuntu 24.04 to ensure accurate timestamps and date handling. It describes checking and updating both server and PHP timezone settings, efficiently updating configuration files, restarting Apache, and verifying changes through a PHP info page for consistent application performance.

This article explains how to set up your system timezone in PHP with Apache on Ubuntu 24.04.

PHP uses the server’s timezone settings to generate timestamps and format date and time correctly. If the timezone is incorrect, it can lead to unexpected results in your applications.

Many applications heavily rely on time-sensitive user interactions. If your application interacts with a database, having the correct timezone ensures that date and time entries are consistent.

The steps below walk you through setting up the correct timezone in PHP with Apache web server.

Set up Ubuntu timezone

Before setting up the timezone for your PHP app, you should ensure your Ubuntu server has the correct timezone since most apps depend on the server’s timezone.

To find out which timezone your server’s is in or configured for, run the command below.

timedatectl

The command above should output a result similar to the one below.

               Local time: Tue 2025-02-18 14:51:49 CST
Universal time: Tue 2025-02-18 20:51:49 UTC
RTC time: Tue 2025-02-18 20:51:50
Time zone: America/Chicago (CST, -0600)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

There’s nothing to do if the server is in the correct time zone.

If not, you can use the steps below to change the server’s timezone.

First, list all the time zones available to your server by running the command below.

sudo timedatectl list-timezones

The command will output a list of all the time zones.

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
.....

Once you find the timezone to use, run the command below to set it up on your server.

sudo timedatectl set-timezone America/Chicago

Replace the highlighted Continent or Country/City in the command to match yours.

Alternatively, you can run the command below to select your server’s time zone interactively.

sudo dpkg-reconfigure tzdata

Use the arrow up/down key to select the correct timezone.

The server’s time zone should be updated.

Set PHP timezone

Now that the server’s timezone is correct set up the PHP timezone to match the server’s.

To change or update the PHP timezone, run the command below to open each php.ini file at these locations.

Replace the highlighted version number (8.3) with the correct PHP version installed on the server.

sudo nano /etc/php/8.3/apache2/php.ini
sudo nano /etc/php/8.3/cli/php.ini

Update the [Date] section in the file.

[Date]
; Defines the default timezone used by the date functions
; https://php.net/date.timezone
date.timezone = America/Chicago

Save the file and exit.

Alternatively, run the command below to quickly update the timezone instead of digging through the php.ini file.

sudo sed -i "s/;date.timezone.*/date.timezone = America\/\Chicago/" /etc/php/8.3/apache2/php.ini
sudo sed -i "s/;date.timezone.*/date.timezone = America\/\Chicago/" /etc/php/8.3/cli/php.ini

Restart PHP to apply the settings.

sudo systemctl restart apache2

Verify the Configuration

You can create a PHP file (e.g., info.php) in Apache’s root directory with the following content to check the current PHP configuration

   <?php
   phpinfo();
   ?>

Access this file through your web browser (http://localhost/info.php) and look for the date.timezone setting to verify it is set correctly.

That should do it!

Conclusion:

Setting the correct timezone in PHP with Apache on Ubuntu is crucial for ensuring that your applications accurately handle time-sensitive data. By following the steps outlined, you can:

  • Verify the server’s current timezone and update it if necessary.
  • Make sure the PHP timezone aligns with the server’s timezone for consistency.
  • Update the php.ini files or use command-line shortcuts for efficiency.
  • Restart the Apache server to apply the changes.
  • Verify the changes by checking the date.timezone setting with a PHP info page.

Ensuring that your system timezone is set correctly is vital for the reliability and accuracy of your web applications.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *