W3docs

PHP Function date_timezone_get()

When working with PHP, it is important to understand the role of timezones in ensuring accurate and consistent date and time displays. The date_timezone_get()

The date_timezone_get() function returns the timezone that is attached to a DateTime object as a DateTimeZone object. Knowing which timezone a date carries is what makes the difference between a timestamp that means "3 PM in New York" and one that means "3 PM somewhere undefined" — and getting that wrong is one of the most common sources of off-by-hours bugs in PHP applications.

This page covers the function's signature, what it returns (including on failure), the object-oriented equivalent you will see more often in real code, and practical examples such as reading and converting a timezone.

Understanding Timezones in PHP

A timezone is a geographical region that shares the same standard time. Each one is identified by a unique IANA name such as America/New_York or Europe/London. PHP ships with the full IANA database; you can list every supported name with timezone_identifiers_list().

Every DateTime object carries a timezone internally. If you do not specify one, it falls back to the script's default timezone — the value set by date_default_timezone_set() (or, failing that, the date.timezone directive in php.ini). To change the timezone of an existing object instead of reading it, use date_timezone_set().

A key point: date_timezone_get() does not convert the time. It only reports the timezone label currently attached to the object. To actually shift the wall-clock value to another zone, you change the timezone with setTimezone() / date_timezone_set().

The date_timezone_get() Function

date_timezone_get() is the procedural alias of the DateTime::getTimezone() method. Both do the same thing — return the DateTimeZone instance held by a DateTime (or DateTimeImmutable) object.

Syntax

date_timezone_get(DateTimeInterface $object): DateTimeZone|false

The object-oriented equivalent:

$object->getTimezone();

Parameters

  • $object: A DateTime or DateTimeImmutable object to read the timezone from.

Return Value

On success it returns a DateTimeZone object. It returns false on failure (for example, if the object has no timezone information attached). Call getName() on the result to get the IANA string such as "Europe/London".

Examples

Here are some examples of how the date_timezone_get() function can be used:

Example 1: Retrieving the Timezone of the Current Date and Time

php— editable, runs on the server

A DateTime created without an explicit timezone inherits the script default, so the output here is whatever was passed to date_default_timezone_set()UTC in this example.

Example 2: Retrieving the Timezone After Changing It

php— editable, runs on the server

After setTimezone() reassigns the zone, date_timezone_get() reports the new value, America/New_York.

Example 3: Reading the Offset and Converting Between Zones

Because date_timezone_get() returns a full DateTimeZone object, you can pass it on to other timezone-aware calls — for example, to read the UTC offset or to convert a moment from one zone to another:

<?php
// 9:00 AM in London on a summer date (BST, UTC+1)
$london = new DateTime('2023-07-01 09:00:00', new DateTimeZone('Europe/London'));

$tz = date_timezone_get($london);
echo $tz->getName(), "\n";                  // Europe/London
echo $tz->getOffset($london) / 3600, "\n";  // 1  (hours east of UTC)

// Convert the same instant to Tokyo time
$london->setTimezone(new DateTimeZone('Asia/Tokyo'));
echo $london->format('Y-m-d H:i'), "\n";    // 2023-07-01 17:00

The instant is identical; only the wall-clock representation changes — London's 09:00 BST is Tokyo's 17:00 JST. See date_format() for the formatting characters used above.

Common Pitfalls

  • It reads, it does not convert. date_timezone_get() only returns the attached zone. Reassigning the result to another variable will not change the date — use setTimezone() for that.
  • Default vs. explicit timezone. An object built without a timezone inherits the script default. If that default is wrong, every read looks correct but the underlying time is off. Set it once with date_default_timezone_set().
  • Prefer the OOP form in modern code. $object->getTimezone() is the same call and reads more naturally inside method chains.

Conclusion

date_timezone_get() returns the DateTimeZone attached to a DateTime object, giving you the IANA name and the tools to read offsets or convert between zones. Combined with date_default_timezone_set() and date_timezone_set(), it lets you keep timestamps unambiguous across users and servers in different parts of the world.

Practice

Practice
What is the purpose of using the date_timezone_get() function in PHP?
What is the purpose of using the date_timezone_get() function in PHP?
Was this page helpful?