Wednesday, November 18, 2020

PHP | date_offset_get() Function

PHP date_offset_get() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP date_offset_get() Function.

PHP date_offset_get() function is an alias of DateTime::getoffset(). This function accepts an object of the class DateTime and return 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 built-in 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 rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP date_parse() Function.

Do feel free to ask your questions where necessary and i 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.
Share:

0 comments:

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.