We now have a youtube channel. Subscribe!

PHP | date_offset_get() Function

PHP date_offset_get() Function

Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_offset_get() Function.

PHP date_offset_get() function is an alias of DateTime::getoffset(). This built-in PHP function accepts an object of the class DateTime and returns the timezone offset of the given date.


Following below is the syntax to use this function -


Parameter Details

Sr.NoParameter & Description

object (Mandatory)

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

Return Value

This function returns the timezone offset of the given Date time object. Incase of failure this function returns FALSE.

PHP Version

This function was first introduced as part of core PHP v 5.2.1 and, works with all the later versions.


Following example illustrates the usage of the PHP date_offset_get() function -

   $date = new DateTime();
   //$timeZone = date_default_timezone_get($date);
   $offset = date_offset_get( $date );
   print("Offset: ".$offset);


When the above code is executed, it will produce the following result -

Offset: 0


The following example creates a date with timezone and, retrieves its offset -

   $dateTimeObj = new DateTime('2018-06-15', timezone_open('Indian/Mahe'));
   //Setting the timezone
   $offset = date_offset_get($dateTimeObj);
   print("Timezone Offset: ".$offset);


When the above code is executed, it will produce the following result -

Timezone Offset: 14400


In the following example below, we are printing the offset of various timezones -

   $dateTimeObj1 = new DateTime('2018-06-15', new DateTimeZone('Indian/Mahe'));
   $dateTimeObj2 = new DateTime('2018-06-15', new DateTimeZone('Asia/Kolkata'));
   $dateTimeObj3 = new DateTime('2018-06-15', new DateTimeZone('America/New_York'));
   $dateTimeObj4 = new DateTime('2018-06-15', new DateTimeZone('Asia/Singapore'));


When the above code is executed, it will produce the following result -



Try the following 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"; ?>


When the above code is executed, it will produce the following result -

Returned value is 0
Returned value is 0

Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP date_parse() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain