PHP Timezones- How to Set/Change Timezone, With Examples

Managing PHP Timezones

This article provides a combined tutorial for the PHP timezone functions, showing how to change the timezone globally or temporarily for a script/object.

Timezones are a huge hassle. If your app has users from around the world, you’ll eventually run into the annoying task of ensuring that date handling works effectively for everyone – including the accuracy of times displayed and the format they are displayed in.

PHP provides several tools to make timezone handling easier, allowing you to set the default timezone for the system, a script, or a specific DateTime object.

PHP DateTime Objects

This article will refer to PHP DateTime objects. These variables contain multiple pieces of information about a given date/time – the time itself and the timezone it is in.

List of Supported Timezones

When setting your timezone, only valid timezone values are allowed (For example, ‘Europe/Amsterdam’). It’s a long list – so rather than reproduce it, here it is in the official PHP docs:

https://www.php.net/manual/en/timezones.php

Finding Out What Timezone Will be Used in a PHP Script

To find out what timezone will be used in a script, run the following function anywhere in the script:

date_default_timezone_get()

This function accepts no parameters and will return the timezone used for DateTime objects in the currently running PHP script.

In order of precedence, the timezone to be used is either:

  • The timezone set using date_default_timezone_set()
  • The timezone is configured in the PHP date.timezone ini option (usually via the php.ini configuration file)
  • If neither of the above is properly set, all dates will default to the UTC timezone

Here it is in action:

echo date_default_timezone_get();

Which will output (obviously with your configured timezone):

Europe/Amsterdam

Setting the Default Timezone for a Script

The following function will set the timezone for the script it is run in only. It should be used towards the beginning of the script before any DateTime objects are created to be all created with the same default timezone. The syntax is as follows:

date_default_timezone_set ( $timezoneId )

This function accepts a single parameter – a timezone from the list of supported timezones. It would return true if it successfully sets the timezone or false if an invalid timezone was supplied.

date_default_timezone_set('America/Los_Angeles');

echo date_default_timezone_get();

Above, the script’s timezone has been changed, and the change is confirmed using date_default_timezone_get().

The change made by calling this function is not permanent and does not apply to anything outside of the script’s execution.

Setting Timezone for the System Globally in php.ini

If you wish to change the timezone globally in PHP so that the default timezone for all PHP scripts executed on the system is changed, you will need to edit the php.ini file.

Note that it is common for there to be a configuration file for both the CLI (Command Line Interface) and a separate configuration file used by the webserver serving PHP applications (e.g., Apache).

Make sure you change the one you need to change or both. Their location will depend on your system configuration but usually can be found in /etc/php/ on Linux.

Add or update the line:

date.timezone = "America/Los_Angeles"

You may need to reboot your web server to see the change take effect.

You can confirm the change affects PHP globally by using phpinfo().

Setting the Timezone for a Specific PHP DateTime Object

Finally, it’s possible to change the timezone for a specific PHP DateTime object.

When created and when otherwise not specified, PHP dates/times are created in the default timezone.

It is possible to specify the timezone while creating a DateTime object by passing it as the second parameter:

$date = new DateTime('2021-01-01', new DateTimeZone('America/Los_Angeles'));
echo $date->format('Y-m-d H:i:sP');

It is then possible to change the timezone of an existing DateTime object using the setTimezone() method:

$date->setTimezone(new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:sP');

Note that when the timezone is changed, the time is changed – if the time value is 00:00 before the change, it will be different afterward, reflecting the equivalent time in the other timezone.

If you wish to change the timezone but not have the time change stored – check out Carbon PHP below for advanced time handling.

Carbon PHP Makes it Easier

If you are building a complex application, I recommend checking out Carbon.

Carbon extends the PHP DateTime object and provides many tools for handling dates, times, timezones, time differences, converting times from one timezone to another, and more.

It makes life easier and allows you to be more confident that dates/times are being handled correctly for your user, regardless of where they are. Find it at:

https://carbon.nesbot.com/

Or, if you’re using Laravel – it’s already installed. Laravel has Carbon built-in, and all dates provided through Laravel are already Carbon objects.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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