PHP Function Reference

PHP date_offset_get() Function



The PHP date_offset_get() function returns the timezone offset of the given date. This function is an alias of DateTime::getOffset() 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 date_offset_get() function.

<?php
  //creating a DateTime object
  $date = date_create("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 function is used to get the timezone offset of a DateTime object.

<?php
  //creating a DateTime object
  $date1 = date_create("25-Jun-2015", 
               new DateTimeZone('America/Chicago'));
  $date2 = date_create("25-Dec-2015", 
               new DateTimeZone('America/Chicago'));

  //getting timezone offset in summer
  $summer = date_offset_get($date1);
  echo "Timezone offset: ".$summer."\n";

  //getting timezone offset in winter 
  $winter = date_offset_get($date2);
  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 function can be used to get the timezone offset of a DateTimeImmutable object. Consider the example below:

<?php
  //creating a DateTimeImmutable object
  $date1 = date_create_immutable("25-Jun-2015", 
               new DateTimeZone('Europe/Paris'));
  $date2 = date_create_immutable("25-Dec-2015", 
               new DateTimeZone('Europe/Paris'));

  //getting timezone offset in summer
  $summer = date_offset_get($date1);
  echo "Timezone offset: ".$summer."\n";

  //getting timezone offset in winter 
  $winter = date_offset_get($date2);
  echo "Timezone offset: ".$winter."\n";  
?>

The output of the above code will be:

Timezone offset: 7200
Timezone offset: 3600

❮ PHP Date and Time Reference