Set Date Timezone in PHP: DateTime, date_default_timezone_set, INI.

How to set a Date Timezone in PHP

There are 3 ways to set a date timezone in PHP. You can use the default_timezone_set, DateTime object and modifying the INI file. The right choice depends on whether you want to update timezone globally, on app level or on an object.

Set Default Timezone in PHP using the date_default_timezone_set function

date_default_timezone_set("America/New_York");
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?

You can change a date’s current Timezone in PHP using the DateTime object. You can also modify all default dates by updating the php.ini file or updating the date_default_timezone_set function.

Click here for all available PHP timezones.

How to set the Default Date Timezone in PHP in php.ini

There are 3 steps to set the default date timezone in PHP on the php.ini file. You need to locate the correct php.ini file and then modify the date.timezone property in the file. Once complete, you’ll need to restart your apache server.

  1. find the correct php.ini file
  2. modify the date.timezone property
  3. restart apache

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.

Updating the Default Date Timezone via the php.ini file Detailed Steps

  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.

date.timezone

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 is set.

timezone in PHP

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

How to set the Date Timezone in PHP Using date_default_timezone_set 

Setting a default timezone for currently running app with date_default_timezone_set is a pretty quick process. You need to get the correct timezoneID constant and pass it into the function.

We globally updated the date timezone in the previous example across the entire server, but this could be problematic if you run multiple apps on the same server. What if you want to update it on a single app in the same server?

Use the date_default_timezone_set function to update at an app level!

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.

date_default_timezone_set("America/New_York");
echo date_default_timezone_get(); //America/New_York

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

How to set the timezone in PHP on a DateTime Object

You can set a PHP DateTime object’s timezone using the DateTimeZone Object. You can set it when you create the object as a second parameter or update it on a pre-existing function.

Ok, we’ve updated the timezone globally across the entire server. We also have updated it within the app, but you may also come across another use case: Updating specific DateTime objects.

In many PHP applications, you are getting Date objects from your database or an API source. The date may come in UTC and need to modify it to a local time for the user. You won’t be able to do it with the above methods.

Set the TimeZone for a specific 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.

Conclusion

Wrap Up

This article explores how to change PHP timezone. We showcase 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.


© 2022 Confident.Systems