3 Ways to Set the Date Timezone in PHP

How to set the Date Timezone in PHP

Here’s a featured snippet from the article that sets the date timezone in PHP.

echo date_default_timezone_get(); //America/New_York

That’s one way of doing it. Explore other options by reading this article till the end.

Context: PHP Default Date Timezone 

A web server uses a bunch of configuration options under the hood. A server running PHP uses a configuration file – php.ini. The configuration file sets up all the necessary directives, including environment variables, timeouts, session and database configs. Some of the configurations vary based on the host’s location.

The date and timezone may vary based on the server’s default configuration. An Apache server running PHP reads the configuration file and lists them under PHPInfo

set the date timezone in PHP

Let’s peek into PHPInfo and see the timezone in our case.

setting PHP timezone

So, the default timezone is “Europe/Berlin.” But this setting could be changed, which is what the article is about.

How to Change PHP Date Timezone?

The article includes a couple of different ways to set a PHP timezone. The list of timezones is available at php.net. So, let’s jump to the practical stuff without any further ado.

Option1: How to set the Date Timezone in PHP in php.ini

As mentioned already, the php.ini file is the heart of all these configurations. However, to change the server’s timezone settings, you need to access the correct php.ini file to change the server’s timezone settings. 

PHP includes a php.ini file for CLI that runs on every invocation. So, to access the php.ini file that your server reads, you can do the following.

  1. Navigate to PHPInfo in the server dashboard.
set the date timezone in PHP

2. In PHPInfo, search for “Loaded Configuration File.”

set the date timezone in PHP

This property gives you the correct path to the server’s php.ini file, which in our case is “/opt/lampp/etc/php.ini.”

Now, as we know the correct path to the file, the next step is open the file and do the following.

1. Search for the “date.timezone” property.


2. Modify “Europe/Berlin” to the timezone of your choice. We will change it to “America/New_York.”

chane PHP timezone

3. If the server is running, you need to restart it.

php date set timezone

Let’s recheck the server’s default timezone with all that set.

timezone in PHP

Here we go! The timezone is now “America/New_York.”

Option2: How to set the Date Timezone in PHP Using date_default_timezone_set 

PHP date_default_timezone_set function alters the default timezone programmatically. Setting PHP timezone this way helps the date/time functions to work in the intended timezone. All you need is to call this function at the start of the PHP script and pass in a timezoneID.

The following example shows how to use PHP date_default_timezone_set.

echo date_default_timezone_get(); //America/New_York

Voila! All the subsequent date/time calls will use America/New_York timezone.

Option3: How to set the Date Timezone in PHP in DateTime Object

PHP DateTime class is designed for representing dates and times. It features alot of useful functions for working with these objects. You can specify a timezone for the object as follows.

$date = new DateTime('2022-08-22', new DateTimeZone('America/New_York'));
echo $date->getTimezone()->getName(). "\n"; //America/New_York

This example uses an object-oriented approach. It passes a DateTimeZone object to the constructor of DateTime. Another approach uses a function to change PHP timezone on the fly. The following example uses that approach.

$date = new DateTime('2022-08-22', new DateTimeZone('America/New_York'));
echo $date->getTimezone()->getName(). "\n"; //America/New_York
date_timezone_set($date, timezone_open('America/Los_Angeles'));
echo $date->getTimezone()->getName(). "\n"; //America/Los_Angeles

Perfect! PHP date timezone classes are robust enough to accommodate changes on the fly. 

I guess we are good to go on setting PHP timezone! Time for a wrap-up.


Wrap Up

This article explores how to change PHP timezone. It features three different options for setting PHP timezone. The first option alters the “date.timezone” in the configuration file and sets up a default timezone for the server. The second option calls the PHP date_default_timezone_set function to change the timezone in a script programmatically.

Finally, the article includes a section on DateTime objects and how to change their timezones in PHP. That’s all for this article. Hopefully, you’ve enjoyed this article. Stay tuned for more at FuelingPHP.

Classes & Functions Mentioned

date_default_timezone_set: (PHP 5 >= 5.1.0, PHP 7, PHP 8) A core PHP function that sets date/time in a script. This function has been available since PHP 5.1.0 and remains stable to this date. The function is likely to remain in use in future as well.

DateTime: (PHP 5 >= 5.2.0, PHP 7, PHP 8) The DateTime class in PHP represents dates/times and features many useful functions to easily manipulate the objects. It was included in PHP 5.2.0 and remained in use. Given its utility and stability, the depreciation seems unlikely.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

  Was this information helpful?

Can Your Career Use a Boost?

Check out our free PHP developer career guide. We created this guide as a resource to help fellow programmers level up their careers. This is an excellent resource whether you are new to programming or have been a professional for more than 15 years.

Click here to read the guide

Article Categories

Article Tags

© 2022 Confident.Systems