We now have a youtube channel. Subscribe!

PHP | date_timezone_set() Function

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

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


Following below is the syntax to use this function -

date_timezone_set($object, $timezone)

Parameter Details

Sr.NoParameter & Description

object (Mandatory)

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


timezone (Mandatory)

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

Return Value

This function returns a DateTime object. In the case of failure, this function 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.


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

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


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

Timezone: Nigeria/Rivers


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

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


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

Timezone: Nigeria/Lagos


Try the following example -

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


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 going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP date() 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