PHP DateTimeZone - getOffset() Method
The PHP DateTimeZone::getOffset() method returns the offset to GMT for the date/time specified by datetime parameter. The GMT offset is calculated with the timezone information contained in the DateTimeZone object being used. The timezone_offset_get() function is an alias of this method.
Syntax
//Object-oriented style public DateTimeZone::getOffset(datetime) //Procedural style timezone_offset_get(object, datetime)
Parameters
object |
Required. For procedural style only: A DateTimeZone object returned by timezone_open(). |
datetime |
Required. Specify the DateTime that contains the date/time to compute the offset from. |
Return Value
Returns time zone offset in seconds.
Example: using both styles
The example below shows the usage of DateTimeZone::getOffset() method.
<?php //creating a DateTimeZone and DateTime object $tz1 = new DateTimeZone("Europe/London"); $date1 = new DateTime("10-May-2015", $tz1); //offset to GMT using Object-oriented style echo "The offset to GMT: ".$tz1->getOffset($date1)."\n"; //creating a DateTimeZone and DateTime object $tz2 = new DateTimeZone("America/Chicago"); $date2 = new DateTime("10-May-2015", $tz2); //offset to GMT using Procedural style echo "The offset to GMT: " .timezone_offset_get($tz2, $date2)."\n"; ?>
The output of the above code will be:
The offset to GMT: 3600 The offset to GMT: -18000
❮ PHP Date and Time Reference