PHP | date_timezone_set() Function
Last Updated :
17 Sep, 2018
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:
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
$date = date_create( '2018-09-15' , timezone_open( 'Asia/Kolkata' ));
echo date_format( $date , 'd-m-Y H:i:sP' ) . "\n" ;
date_timezone_set( $date , timezone_open( 'Asia/Singapore' ));
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
$date = new DateTime( '2018-09-15' , new DateTimeZone( 'Asia/Kolkata' ));
echo $date ->format( 'd-m-Y H:i:sP' ) . "\n" ;
$date ->setTimezone( new DateTimeZone( 'Asia/Singapore' ));
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...