Thursday, November 19, 2020

PHP | date_timezone_set() Function

PHP date_timezone_set() 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 date_timezone_set() Function in PHP.

PHP date_timezone_set() function accepts a DateTime object and a timezone object as parameters and, set the specified timezone to the given DateTime object.

Syntax

Following below is the syntax to use this function -

date_timezone_set($object, $timezone)


Parameter Details

Sr.NoParameter & Description
1

object (Mandatory)

This represents the DateTime object for which you need to set the timezone.

2

timezone (Mandatory)

This is a TimeZone object representing the time zone you need to set to the DateTime object.


Return Value

This built-in function returns a DateTime object. Incase of failure, it returns the boolean value FALSE.

PHP Version

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

Example1

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

<?php
   $date = date_create("25-09-1989"); 
   $tz = new DateTimeZone('Nigeria/Rivers');   
   $res = date_timezone_set($date, $tz);   
   print("Timezone: ".timezone_name_get(date_timezone_get($date)) );
?>

Output

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

Timezone: Nigeria/Rivers

Example2

The following example creates a DateTime object along with the timezone and, sets the time zone to another value -

<?php
   $date = new DateTime("25-09-1989", new DateTimeZone('Nigeria/Rivers')); 
   $res = date_timezone_set($date, timezone_open("Nigeria/Lagos"));   
   print("Timezone: ".timezone_name_get(date_timezone_get($date)) );
?>

Output

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

Timezone: Nigeria/Lagos

Example3

Try the following example -

<?php
   $dateSrc = '2007-04-19 12:50 GMT';
   $dateTime = date_create( $dateSrc);
   $DateTimeZone = timezone_open ( 'America/Chicago' );
   date_timezone_set( $dateTime, $DateTimeZone );
   $NewDateTimeZone = date_timezone_get($dateTime);   
   echo 'New timeZone is '. timezone_name_get($NewDateTimeZone);
   echo "\n";
   # Using second function.
   $dateTime = new DateTime($dateSrc);
   $DateTimeZone = timezone_open ( 'America/Chicago' );
   $dateTime->setTimezone( $DateTimeZone );
   $NewDateTimeZone = $dateTime->getTimezone ();   
   echo 'New timeZone is '. timezone_name_get ($NewDateTimeZone);
?>

Output

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

New timeZone is America/Chicago
New timeZone is America/Chicago


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() 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.