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'); ?> |
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'); ?> |
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
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
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.

