Timezones look simple until an application starts serving users from different parts of the world. A date that looks correct on a developer’s machine can suddenly be several hours wrong when it reaches another user. Many developers discover this only after a “scheduled for 10 AM” feature runs at the wrong time.
PHP provides built-in timezone support through the DateTime and DateTimeZone classes. Using these APIs correctly helps you store, display, and convert dates reliably without manually calculating offsets.
In this article, we will see how to set the PHP timezone, get the current timezone, list supported timezone identifiers, and convert dates between different timezones.
Understanding PHP Timezone Configuration
PHP uses a default timezone when working with date and time functions. If a timezone is not configured, PHP may use the server timezone or show a warning depending on the configuration. If you are new to PHP date handling, see our guide on dates and time in PHP for an overview of commonly used date functions.
You can configure the default timezone using the date_default_timezone_set() function.
<?php
date_default_timezone_set("Asia/Kolkata");
echo date("Y-m-d H:i:s");
?>
The output will use the configured timezone instead of the server’s default timezone.
The timezone identifier Asia/Kolkata follows the IANA timezone database format. PHP supports hundreds of such identifiers, including regions like America/New_York, Europe/London, and Australia/Sydney.
You can find the complete list of supported timezone identifiers in the official PHP documentation for supported timezones.
Setting PHP Timezone in Different Ways
There are multiple places where you can configure a timezone depending on your application requirement.
1. Set timezone in PHP code
For most applications, setting the timezone at the beginning of your PHP execution is enough.
<?php
date_default_timezone_set("America/New_York");
echo date("l, d M Y H:i:s");
?>
This affects PHP date-related functions such as date(), time(), and strtotime().
2. Set timezone in php.ini
If you control the server configuration, you can define the default timezone globally in php.ini.
date.timezone = "Asia/Kolkata"
After changing php.ini, restart the web server or PHP service for the change to take effect.
3. Set timezone using the DateTimeZone class
For application-level date handling, using DateTimeZone is usually a better approach because it keeps timezone handling close to the date object.
<?php
$date = new DateTime("now", new DateTimeZone("Europe/London"));
echo $date->format("Y-m-d H:i:s");
?>
This approach avoids changing the global timezone setting when you only need a specific timezone for a particular operation.
Getting the Current PHP Timezone
You can retrieve the currently configured PHP timezone using the date_default_timezone_get() function.
<?php
echo date_default_timezone_get();
?>
For example, if the application timezone is configured as Asia/Kolkata, the output will be:
Asia/Kolkata
Checking the configured timezone is useful when debugging date-related issues, especially on shared hosting or production servers where the server configuration may not match your local development environment.
Using DateTimeZone to Work with Timezones
The DateTimeZone class provides an object-oriented way to handle timezone information in PHP. It works together with the DateTime class for creating and converting dates.
A common practice is to create dates in UTC and convert them to the user’s timezone when displaying them. This avoids many timezone-related problems in applications with users from different regions.
Create a DateTime object with a specific timezone
<?php
$timezone = new DateTimeZone("America/New_York");
$date = new DateTime("now", $timezone);
echo $date->format("Y-m-d H:i:s");
?>
The timezone is attached to the date object, so PHP automatically applies the correct offset.
Converting a Date Between Timezones in PHP
PHP allows you to convert an existing date to another timezone using the setTimezone() method.
For example, the following code converts a UTC date into the Indian Standard Time timezone.
<?php
$date = new DateTime("2026-07-23 10:00:00", new DateTimeZone("UTC"));
$date->setTimezone(new DateTimeZone("Asia/Kolkata"));
echo $date->format("Y-m-d H:i:s");
?>
Output:
2026-07-23 15:30:00
Notice that the actual moment in time does not change. Only the displayed timezone changes.
When displaying converted dates in different formats, you can also refer to this guide on formatting dates in PHP.
This distinction is important. Timezone conversion is not changing the event time. It is showing the same moment according to another region’s clock.
Listing All Supported PHP Timezones
PHP provides the DateTimeZone::listIdentifiers() method to retrieve all available timezone identifiers.
<?php
$timezones = DateTimeZone::listIdentifiers();
foreach ($timezones as $timezone) {
echo $timezone . "<br>";
}
?>
This is useful when building forms where users can select their preferred timezone.
For example, a user profile page may store the selected timezone identifier and use it whenever dates are displayed.
Finding Timezone Offset
Sometimes you may need the UTC offset for a timezone. The getOffset() method returns the offset in seconds.
<?php
$timezone = new DateTimeZone("Asia/Kolkata");
$date = new DateTime("now", $timezone);
echo $timezone->getOffset($date);
?>
The output for India Standard Time is:
19800
This represents 19,800 seconds, which equals UTC +5:30.
In most applications, you should avoid manually calculating offsets. Let PHP handle timezone rules because some regions have daylight saving time changes.
Best Practices for Handling Timezones in PHP
Timezone issues are rarely caused by the PHP functions themselves. They usually happen because an application does not have a consistent strategy for storing and displaying dates.
The following practices help avoid common timezone problems.
Store dates in UTC
For applications used by people in multiple regions, storing dates in UTC is usually the safest approach.
For storing and comparing time values, understanding Unix timestamps can also be useful. See our guide on PHP timestamps.
For example, a meeting scheduled at a specific moment can be stored as:
2026-07-23 10:00:00 UTC
When displaying the meeting, convert it into the user’s selected timezone.
This avoids storing multiple versions of the same event and keeps database values consistent.
Store timezone identifiers, not offsets
Avoid storing values like +05:30 or -04:00 as a user’s timezone preference.
Instead, store the IANA timezone identifier:
Asia/Kolkata
America/New_York
Europe/London
A timezone identifier contains regional rules, including daylight saving time changes where applicable. A fixed offset does not.
Avoid changing the global timezone repeatedly
The date_default_timezone_set() function changes the timezone for the entire PHP request. It is useful for application configuration, but repeatedly changing it in different parts of your code can make debugging difficult.
For isolated conversions, prefer creating a DateTimeZone object and passing it to DateTime.
Use immutable dates when possible
PHP also provides the DateTimeImmutable class. Unlike DateTime, it does not modify the existing object when a date operation is performed.
<?php
$date = new DateTimeImmutable(
"now",
new DateTimeZone("UTC")
);
$newDate = $date->setTimezone(
new DateTimeZone("Asia/Kolkata")
);
echo $date->format("Y-m-d H:i:s");
echo "<br>";
echo $newDate->format("Y-m-d H:i:s");
?>
Immutable objects can reduce accidental date changes when the same date value is reused in multiple places.
Common PHP Timezone Errors and Fixes
Warning: date_default_timezone_get(): It is not safe to rely on the system’s timezone settings
This warning appears when PHP cannot determine the configured timezone.
Fix it by setting a default timezone:
<?php
date_default_timezone_set("UTC");
?>
You can also configure it in php.ini.
The displayed time is different between local and production servers
This usually happens when the development machine and production server use different timezone settings.
Check the current timezone on both environments:
<?php
echo date_default_timezone_get();
?>
It is a good practice to explicitly configure the timezone instead of depending on server defaults.
Daylight saving time creates unexpected results
Some regions change their clock during the year. Manually adding or subtracting hours can produce incorrect results during these transitions.
Use named timezone identifiers and allow PHP to apply the correct rules automatically.
PHP Timezone FAQ
What is the default timezone in PHP?
PHP uses the timezone configured in php.ini. If it is not configured, PHP may fall back to the server timezone and display a warning depending on the environment.
How do I set UTC timezone in PHP?
Use the following code:
<?php
date_default_timezone_set("UTC");
?>
Should I store timezone in the database?
If users can have different timezone preferences, store their timezone identifier separately, such as Asia/Kolkata or Europe/London. Store actual event timestamps in UTC.
What is the difference between DateTime and DateTimeZone in PHP?
DateTime represents a date and time value, while DateTimeZone represents the timezone rules used to interpret that date and time.
Conclusion
Handling timezones correctly is an important part of building reliable PHP applications. The safest approach is to avoid manual timezone calculations and use PHP’s built-in DateTime and DateTimeZone features.
Configure a clear application timezone, store dates consistently in UTC, and convert them only when displaying them to users. This simple approach prevents many of the common date and time bugs that appear when an application grows.
If you are working with user-specific dates, scheduled tasks, or international applications, timezone handling should be treated as part of your application design rather than an afterthought.