The getOffset Method of the DateTime Class
The getOffset
method of the DateTime
class returns the timezone offset
in seconds relative to GMT (UTC) time. This is useful when working with different
time zones. The method does not take any parameters and returns an integer.
Syntax
$datetime->getOffset();
Example
Let's get the offset for the current time in the Europe/Moscow timezone:
<?php
$date = new DateTime('now', new DateTimeZone('Europe/Moscow'));
$res = $date->getOffset();
echo $res;
?>
The code execution result (may vary depending on the season):
10800
Example
Let's compare the offsets for different time zones:
<?php
$zones = ['UTC', 'America/New_York', 'Asia/Tokyo'];
foreach ($zones as $zone) {
$date = new DateTime('now', new DateTimeZone($zone));
echo $zone . ': ' . $date->getOffset() . "\n";
}
?>
The code execution result:
UTC: 0
America/New_York: -18000
Asia/Tokyo: 32400
See Also
-
the
date
function,
which formats date and time -
the
date_default_timezone_set
function,
which sets the default timezone