Friday, October 17, 2025
HomeLanguagesPHP | date_timezone_set() Function

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:




<?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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS