+

PHP Date_timezone_set () function

Syntax:
  • Procedural style:
    date_timezone_set ($object, $timezone) 
  • Object oriented style:
    DateTime::setTimezone ($timezone) 
Parameters: this function takes two parameters as above and described below:
  • $object:this is a required parameter that is used to specify the DateTime object returned by the date_create ( ).
  • $timezone:This parameter is used to set the DateTimeZone object representing the desired time zone.
Returned value: this function returns a DateTime object on success, or False on error.The following programs illustrate the date_timezone_set() function in PHP:Program 1:  
// Create DateTime object $date = date_create ( `2018-09-15` , timezone_open ( ` Asia / Kolkata` ));  
// Show date format echo date_format ( $date , `dmY H: i: sP` ). "" ;  
// Set date, time zone date_timezone_set ( $date , timezone_open ( `Asia / Singapore` ));  
// Show date format echo date_format ( $date , `dmY H: i: sP` ); ?>
Exit:
15-09 -2018 00: 00: 00 + 05: 30 15-09-2018 02: 30: 00 + 08: 00 
Program 2: < / p>  
// Create a DateTime object $date = new DateTime ( `2018-09-15` , new DateTimeZone ( `Asia / Kolkata` ));  
// Show date format echo $date -> format ( `dmY H: i: sP` ). "" ;  
// Set date, time zone $date -> setTimezone ( new DateTimeZone ( `Asia / Singapore` ));  
// Show date format echo $date -> format ( `dmY H: i: sP` ); ?>
Exit:
15-09 -2018 00: 00: 00 + 05: 30 15-09-2018 02: 30: 00 + 08: 00 
Related Articles:
  • PHP | Time_nanosleep() function
  • PHP | Timezone_name_from_abbr() function
Link: http://php.net/manual/en/datetime.settimezone.php
Get Solution for free from DataCamp guru