PHP DateTime - getOffset() Method
The PHP DateTime::getOffset() method returns the timezone offset of the given date. The date_offset_get() function is an alias of this method.
Note: This method is defined under DateTimeInterface interface. DateTime and DateTimeImmutable classes inherit this method from DateTimeInterface interface.
Syntax
//Object-oriented style public DateTime::getOffset() public DateTimeImmutable::getOffset() public DateTimeInterface::getOffset() //Procedural style date_offset_get(object)
Parameters
object |
Required. For procedural style only: A DateTime object returned by date_create(). |
Return Value
Returns the timezone offset in seconds from UTC on success.
Example: using both styles
The example below shows the usage of DateTime::getOffset() method.
<?php //creating a DateTime object $date = new DateTime("25-Jun-2015", new DateTimeZone('Europe/London')); //getting the timezone offset using Object-oriented style $summer = $date->getOffset(); echo "Timezone offset: ".$summer."\n"; //getting the timezone offset using Procedural style $summer = date_offset_get($date); echo "Timezone offset: ".$summer."\n"; ?>
The output of the above code will be:
Timezone offset: 3600 Timezone offset: 3600
Example: Timezone offset of a DateTime object
Consider the example below where this method is used to get the timezone offset of a DateTime object.
<?php //creating a DateTime object $date1 = new DateTime("25-Jun-2015", new DateTimeZone('America/Chicago')); $date2 = new DateTime("25-Dec-2015", new DateTimeZone('America/Chicago')); //getting timezone offset in summer $summer = $date1->getOffset(); echo "Timezone offset: ".$summer."\n"; //getting timezone offset in winter $winter = $date2->getOffset(); echo "Timezone offset: ".$winter."\n"; ?>
The output of the above code will be:
Timezone offset: -18000 Timezone offset: -21600
Example: Timezone offset of a DateTimeImmutable object
Similarly, this method can be used to get the timezone offset of a DateTimeImmutable object. Consider the example below:
<?php //creating a DateTimeImmutable object $date1 = new DateTimeImmutable("25-Jun-2015", new DateTimeZone('Europe/Paris')); $date2 = new DateTimeImmutable("25-Dec-2015", new DateTimeZone('Europe/Paris')); //getting timezone offset in summer $summer = $date1->getOffset(); echo "Timezone offset: ".$summer."\n"; //getting timezone offset in winter $winter = $date2->getOffset(); echo "Timezone offset: ".$winter."\n"; ?>
The output of the above code will be:
Timezone offset: 7200 Timezone offset: 3600
❮ PHP Date and Time Reference