Ubuntu Linux

Setup PHP Timezone in Nginx on Ubuntu 24.04

Richard
Written by
Richard
Feb 18, 2025 Updated Mar 20, 2026 5 min read
Setup PHP Timezone in Nginx on Ubuntu 24.04

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

PHP uses the server’s timezone settings to generate timestamps and format date and time correctly. Having the correct timezone configuration ensures that your PHP applications’ date and time functions return accurate results.

Many applications heavily rely on time-sensitive user interactions. Having accurate timestamps in logs is vital for debugging and monitoring.

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

Set up Ubuntu timezone

Before setting up the timezone for your PHP app, 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.

💻Code
timedatectl

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

💻Code
               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.

🐧Bash / Shell
sudo timedatectl list-timezones

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

💻Code
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.

🐧Bash / Shell
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.

🐧Bash / Shell
sudo dpkg-reconfigure tzdata

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

Terminal output displaying server timezone selection options on Ubuntu 24.04

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.

🐧Bash / Shell
sudo nano /etc/php/8.3/fpm/php.ini
sudo nano /etc/php/8.3/cli/php.ini

Update the [Date] section in the file.

🐘PHP
[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.

🐧Bash / Shell
sudo sed -i "s/;date.timezone.*/date.timezone = America/Chicago/" /etc/php/8.3/fpm/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.

🐧Bash / Shell
sudo systemctl restart nginx
sudo systemctl restart php8.3-fpm

Verify the Configuration

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

🐘PHP
   <?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.

Server timezone selection timezone

If your test doesn’t show the PHP test page, you must set up a PHP module in Nginx.

Open the default Nginx server block by running the command below.

🐧Bash / Shell
sudo nano /etc/nginx/sites-available/default

Then, edit the highlighted lines in the file exit and save it.

🐘PHP
        # pass PHP scripts to FastCGI server
#
location ~ .php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

Restart Nginx and test again.

That should do it!

Conclusion:

  • Setting the correct timezone in PHP and on your Ubuntu server is essential for accurate date and time handling in your applications.
  • Ensure that your server’s timezone is configured correctly to prevent discrepancies in timestamps.
  • Updating the php.ini file or using the sed command provides a quick way to set the PHP timezone without manual edits.
  • Verifying the configuration with a simple PHP info script helps confirm that the changes have been applied successfully.
  • Regular checks and maintenance of timezone settings can enhance the reliability of time-sensitive applications.

Frequently Asked Questions

How do I check the current timezone on my Ubuntu server?

You can check the current timezone on your Ubuntu server by running the command 'timedatectl'. This command will display the local time, universal time, and the configured timezone.

What command do I use to set the timezone on Ubuntu?

To set the timezone on Ubuntu, use the command 'sudo timedatectl set-timezone '. Replace '' with the desired timezone, such as 'America/Chicago'.

How can I update the PHP timezone in Ubuntu?

To update the PHP timezone, you need to edit the php.ini file located at '/etc/php//fpm/php.ini' and '/etc/php//cli/php.ini'. Change the 'date.timezone' setting to your desired timezone, such as 'America/Chicago'.

Is it necessary to match PHP timezone with server timezone?

Yes, it is important to match the PHP timezone with the server timezone to ensure that date and time functions return accurate results. This alignment helps avoid discrepancies in time-sensitive applications.

What should I do if my timezone is not listed in the available timezones?

If your desired timezone is not listed, ensure that your system is updated. You can also check for any additional timezone data packages that may need to be installed on your Ubuntu system.

Was this guide helpful?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

2469 articles → Twitter

📚 Related Tutorials

Setup PHP Timezone in Apache on Ubuntu 24.04
Ubuntu Linux Setup PHP Timezone in Apache on Ubuntu 24.04
How to Set System Locale on Ubuntu 24.04
Ubuntu Linux How to Set System Locale on Ubuntu 24.04

Leave a Reply

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