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.

Syntax

Following below is the syntax to use this function -

date_offset_get($object)


Parameter Details

Sr.NoParameter & Description
1

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.

Example1

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

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

Output

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

Offset: 0

Example2

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

<?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);
?>

Output

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

Timezone Offset: 14400

Example3

In the following example below, we are printing the offset 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));
?>

Output

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

14400
19800
-14400
28800

Example4

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"; ?>

Output

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.