English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP date_offset_get() Function

PHP Date & Time Function Manual

The date_offset_get() function returns the timezone offset

Definition and Usage

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

Syntax

date_offset_get($object)

Parameter

Serial NumberParameters and Description
1

object (required)

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



Return value

The date_offset_get() function returns the timezone offset of the given DateTime object. If it fails, this function will return a boolean value.false.

PHP version

This function was originally introduced in PHP version5.2.1introduced in and can be used in all higher versions.

Online example

The following is an example of using the date_offset_get() function-

<?php
$date = new DateTime();
//$timeZone = date_default_timezone_get($date);
$offset = date_offset_get($date);
print("Offset: $offset");
?>
Test and see‹/›

Output result

Offset: 0

Online example

In the following example, we will create a date with a time zone and get its offset-

<?php
   $dateTimeObj = new DateTime('2018-06-15', timezone_open('Indian/', new DateTimeZone('Indian
   //Set time zone
   $offset = date_offset_get($dateTimeObj);
   print("\n");
   print("Time zone offset: $offset");
?>
Test and see‹/›

Output result:

Time zone offset: 14400

Online example

In the following example, we will print the offsets of various time zones.-

<?php
   $dateTimeObj1 = new DateTime('2018-06-15Mahe'));/', new DateTimeZone('Indian
   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));
?>
Test and see‹/›

Output result

14400
19800
-14400
28800

Online example

$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = date_create($dateSrc);;
$retval = date_offset_get($dateTime);   
echo "The return value is $retval";
echo "<br>";
#Use the second method
$dateTime = new DateTime($dateSrc);
$retval = $dateTime->getOffset();   
echo "The return value is $retval";
?>
Test and see‹/›

Output result:

The return value is 0
The return value is 0