Dates7 min readExamples run on PHP 8.5.10

date_offset_get()

Learn how date_offset_get() returns the UTC offset in seconds for a DateTime or DateTimeImmutable, with DST examples, edge cases and common mistakes.

Signature

date_offset_get(DateTimeInterface $object): int
Available since PHP 5.2.07.48.08.18.28.38.48.5All versions →

Purpose

date_offset_get() tells you how far a date and time object is from UTC, measured in seconds. The manual describes it as the procedural alias of DateTime::getOffset(), so both forms give the same answer. Use it when you already have a date object and need to know which offset applies to that particular moment, for example to store the offset next to a timestamp or to display +05:30 in a report.

The offset belongs to the object's own moment in time, not to the server's default timezone. That distinction matters for zones that observe daylight saving time, because the same zone can have different offsets in winter and summer.

Signature

Parameters and defaults

  • $object: a DateTime, a DateTimeImmutable, or any other DateTimeInterface implementation. There is no default value, so you must pass it.

There are no other parameters. The function does not accept a timestamp or a date string. Create an object first, then pass it in.

Return value and failure cases

The function returns an int: the offset from UTC in seconds. The sign is easy to remember because positive numbers are east of UTC and negative numbers are west of it. UTC itself returns 0, India Standard Time returns 19800, and New York in winter returns -18000.

In PHP 8 you should not expect a false return for ordinary use. If you pass something that is not a DateTimeInterface, such as a string or an integer, PHP throws a TypeError. Wrap risky input in a type check or construct the object first so that parse errors are handled at the point where the date is created.

Example 1: UTC has a zero offset

This is the simplest case. The object is explicitly created in UTC, so the offset is zero seconds.

Example
<?php
date_default_timezone_set('UTC');
$date = new DateTimeImmutable('2024-01-15 12:00:00', new DateTimeZone('UTC'));
echo date_offset_get($date), "\n";
Output
0

Example 2: the same zone in winter and summer

A named zone such as America/New_York has no single offset. The result depends on the date stored in the object, so the function reflects daylight saving time automatically.

Example
<?php
date_default_timezone_set('UTC');
$zone = new DateTimeZone('America/New_York');
$winter = new DateTimeImmutable('2024-01-15 12:00:00', $zone);
$summer = new DateTimeImmutable('2024-07-15 12:00:00', $zone);
echo json_encode([date_offset_get($winter), date_offset_get($summer)]);
echo "\n";
Output
[-18000,-14400]

The winter value is -18000 (five hours behind UTC) and the summer value is -14400 (four hours behind).

Example 3: a fixed offset and conversion to hours

When a date string carries its own offset, the object uses it. The result is still in seconds, so divide by 3600 if you want hours. Here we also confirm that the procedural call matches the method.

Example
<?php
date_default_timezone_set('UTC');
$date = new DateTime('2024-03-10 08:00:00+05:30');
$seconds = date_offset_get($date);
echo json_encode(['seconds' => $seconds, 'hours' => $seconds / 3600, 'same' => $seconds === $date->getOffset()]);
echo "\n";
Output
{"seconds":19800,"hours":5.5,"same":true}

Edge cases

  • Offsets are not always whole hours. Values such as 19800 (+05:30) are normal, so do not assume the result divides evenly by 3600.
  • The offset describes one instant. If you change the date with modify() or setDate() on a DateTime, the offset can change when the zone crosses a daylight saving boundary.
  • Immutable objects are fine. DateTimeImmutable implements DateTimeInterface, so it works exactly like DateTime here.
  • Abbreviation and fixed-offset zones are static. An object built with +05:30 keeps that offset for every date, unlike a named region.
  • The default timezone only matters at creation. If you create an object without a zone argument and without an offset in the string, PHP uses the default timezone. Setting UTC explicitly, as in the examples, makes results predictable.

Common mistakes

  1. Passing a timestamp or string. date_offset_get(1700000000) is a type error. Build a DateTime or DateTimeImmutable first.
  2. Reversing the sign. A zone west of UTC gives a negative number. Do not subtract it when you meant to add it, and check the direction when converting between local time and UTC.
  3. Treating the result as hours. The unit is seconds. Forgetting the division by 3600 produces large, confusing numbers.
  4. Assuming one offset per zone name. A region name is a set of rules. Always read the offset from an object that holds the exact date you care about.
  5. Formatting offsets by hand. If you only need +05:30 text for display, format('P') is simpler than computing it from seconds. Use date_offset_get() when you need the numeric value.
  • DateTime::getOffset(): the method form that this function aliases.
  • timezone_offset_get(): finds the offset of a DateTimeZone for a given date object.
  • date_timezone_get(): returns the DateTimeZone attached to an object.
  • date_default_timezone_set(): sets the default zone used when none is supplied.
  • date_create(): procedural way to create a date object to pass in.

Recap

date_offset_get() accepts a date object and returns its UTC offset in seconds as an integer. East of UTC is positive, west is negative. The value depends on the exact moment stored in the object, so daylight saving time is handled for you. Pass a DateTimeInterface, not a string or timestamp, and convert to hours yourself when you need them.

Tested examples

UTC has a zero offset
<?php
date_default_timezone_set('UTC');
$date = new DateTimeImmutable('2024-01-15 12:00:00', new DateTimeZone('UTC'));
echo date_offset_get($date), "\n";
Output
0
The same zone in winter and summer
<?php
date_default_timezone_set('UTC');
$zone = new DateTimeZone('America/New_York');
$winter = new DateTimeImmutable('2024-01-15 12:00:00', $zone);
$summer = new DateTimeImmutable('2024-07-15 12:00:00', $zone);
echo json_encode([date_offset_get($winter), date_offset_get($summer)]);
echo "\n";
Output
[-18000,-14400]
A fixed offset and conversion to hours
<?php
date_default_timezone_set('UTC');
$date = new DateTime('2024-03-10 08:00:00+05:30');
$seconds = date_offset_get($date);
echo json_encode(['seconds' => $seconds, 'hours' => $seconds / 3600, 'same' => $seconds === $date->getOffset()]);
echo "\n";
Output
{"seconds":19800,"hours":5.5,"same":true}