• PHP Video Tutorials

PHP date_offset_get() Function


Advertisements

Definition and Usage

The date_offset_get() is an alias of DateTime::getOffset. This function accepts an object of the class DateTime and returns the timezone offset of the given date.

Syntax

date_offset_get($object)

Parameters

Sr.No Parameter & Description
1

object (Mandatory)

This is a DateTime object for which you need the timezone offset.

Return Values

PHP date_offset_get() function returns the timezone offset of the given DateTime object. Incase of failure, this function returns the boolean value false.

PHP Version

This function was first introduced in PHP Version 5.2.1 and, works with all the later versions.

Example

Following example of the usage of the date_offset_get() function −

Live Demo
<?php
   $date = new DateTime();
   //$timeZone = date_default_timezone_get($date);
   $offset = date_offset_get( $date );
   print("Offset: ".$offset);
?>

This will produce following result −

Offset: 0

Example

In the following example we are creating a date with timezone and, retrieving its offset −

Live Demo
<?php
   $dateTimeObj = new DateTime('2018-06-15', timezone_open('Indian/Mahe'));
   //Setting the timezone
   $offset = date_offset_get($dateTimeObj);
   print("\n");
   print("Timezone Offset: ".$offset);
?>

This will produce the following result −

Timezone Offset: 14400

Example

In the following example we are printing offsets of various timezones. −

<?php
   $dateTimeObj1 = new DateTime('2018-06-15', new DateTimeZone('Indian/Mahe'));
   print(date_offset_get($dateTimeObj1));
   print("\n");
   $dateTimeObj2 = new DateTime('2018-06-15', new DateTimeZone('Asia/Kolkata'));
   print(date_offset_get($dateTimeObj2));
   print("\n");
   $dateTimeObj3 = new DateTime('2018-06-15', new DateTimeZone('America/New_York'));
   print(date_offset_get($dateTimeObj3));
   print("\n");
   $dateTimeObj4 = new DateTime('2018-06-15', new DateTimeZone('Asia/Singapore'));
   print(date_offset_get($dateTimeObj4));
?>

This will produce following result −

14400
19800
-14400
28800

Example

$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = date_create( $dateSrc);;
$retval = date_offset_get( $dateTime);   
echo "Returned value is $retval";
echo "
"; #Using second function. $dateTime = new DateTime($dateSrc); $retval = $dateTime->getOffset(); echo "Returned value is $retval"; ?>

This will produce the following result −

Returned value is 0
Returned value is 0
php_function_reference.htm
Advertisements