Skip to content
Related Articles

Related Articles

Improve Article

PHP | date_timezone_set() Function

  • Last Updated : 17 Sep, 2018
Geek Week

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:




<?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');
?>
Output:
15-09-2018 00:00:00+05:30
15-09-2018 02:30:00+08:00

Program 2:




<?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');
?>
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
Recommended Articles
Page :