Setup PHP Timezone in Nginx on Ubuntu 24.04
Setting the PHP timezone in Nginx on Ubuntu 24.04 tells your web applications which local time to use, instead of the standard UTC. This involves changing a setting in the php.ini file.
This ensures all PHP scripts correctly show dates and times based on your server’s location. For example, if your server is in New York, it should use EDT (Eastern Daylight Time) or EST (Eastern Standard Time) instead of UTC.
Getting the timezone right is important for things like scheduling tasks, showing the correct time for comments or posts, and making sure server logs are accurate. Without the correct setting, your applications might show incorrect times, leading to confusion or errors.
Configure the `php.ini` file to set the `date.timezone` directive. Edit the `php.ini` files located at `/etc/php/VERSION/fpm/php.ini` and `/etc/php/VERSION/cli/php.ini`, replacing `VERSION` with your PHP version. Set `date.timezone = Your/Timezone` and restart PHP-FPM and Nginx.
Set up Ubuntu timezone
Setting your Ubuntu timezone correctly is important so that apps on your server show the right time. You can check your current Ubuntu timezone by running the ‘timedatectl’ command in your terminal. This command shows you if your timezone is already set up or what it’s currently set to.
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
After setting your Ubuntu timezone, you must set your PHP timezone to match so your web apps display the correct local time. To change your PHP timezone, you’ll edit the php.ini file for your PHP version. You can open the correct php.ini file for your Nginx setup on Ubuntu using a specific command.
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/fpm/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.
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.
sudo systemctl restart nginx
sudo systemctl restart php8.3-fpm
Verify the Configuration
Verifying your PHP timezone configuration is easy and ensures your applications are using the correct time. You can check this by creating a simple PHP file that uses the phpinfo() function. This function shows your current date.timezone setting, which you can see by opening the file in your web browser.
<?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.

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.
sudo nano /etc/nginx/sites-available/default
Then, edit the highlighted lines in the file exit and save it.
# 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.inifile or using thesedcommand 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.
Was this guide helpful?
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.
No comments yet — be the first to share your thoughts!