PHP | date_timezone_set() Function

The date_timezone_set() function is an inbuilt function in PHP which is used to sets the time zone for the DateTime object. This function returns the DateTime object or False on failure.

Syntax:

  • Procedural style:
    date_timezone_set( $object, $timezone )
  • Object oriented style:
    DateTime::setTimezone( $timezone )

Parameters: This function accepts two parameters as mentioned above and described below:



  • $object: It is a mandatory parameter which is used to specify the DateTime object which is returned by the date_create() function.
  • $timezone: This parameter is used to set the DateTimeZone object representing the desired time zone.

Return Value: This function returns the DateTime object on success or False on failure.

Below programs illustrate the date_timezone_set() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create DateTime object
$date = date_create('2018-09-15', timezone_open('Asia/Kolkata'));
  
// Display the date format
echo date_format($date, 'd-m-Y H:i:sP') . "\n";
  
// Set the date time zone
date_timezone_set($date, timezone_open('Asia/Singapore'));
  
// Display the date format
echo date_format($date, 'd-m-Y H:i:sP');
?>

chevron_right


Output:

15-09-2018 00:00:00+05:30
15-09-2018 02:30:00+08:00

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create DateTime object
$date = new DateTime('2018-09-15', new DateTimeZone('Asia/Kolkata'));
  
// Display the date format
echo $date->format('d-m-Y H:i:sP') . "\n";
  
// Set the date time zone
$date->setTimezone(new DateTimeZone('Asia/Singapore'));
  
// Display the date format
echo $date->format('d-m-Y H:i:sP');
?>

chevron_right


Output:

15-09-2018 00:00:00+05:30
15-09-2018 02:30:00+08:00

Related Articles:

Reference: http://php.net/manual/en/datetime.settimezone.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.