Open In App

PHP | DateTimeImmutable::setTimezone() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The DateTimeImmutable::setTimezone() function is an inbuilt function in PHP which is used to set the time zone for the created DateTimeImmutable object. This function returns the DateTimeImmutable object or False on failure.

Syntax:

DateTimeImmutable::setTimezone ( TimeZone )

Parameters: This function accepts one parameters which is illustrated below:-

TimeZone: This parameter is used to set the DateTimeZone object representing the desired time zone.

Return Values: This function returns the DateTimeImmutable object on success or False on failure.

Below programs illustrate the DateTimeImmutable::setTimezone() function :

Program 1 :




<?php
// PHP program to illustrate DateTimeImmutable::setTimezone()
// function
    
// Creating a DateTimeImmutable() object 
$DateTimeImmutable = new DateTimeImmutable('2019-10-07', new DateTimeZone('Asia/Kolkata')); 
    
// Getting the above datetime format 
echo $DateTimeImmutable->format('d-m-Y H:i:sP') . "\n"
    
// Calling the DateTimeImmutable::setTimezone() function
$a = $DateTimeImmutable->setTimezone(new DateTimeZone('Asia/Singapore')); 
    
// Getting a new DateTimeImmutable object
echo $a->format('d-m-Y H:i:sP'); 
?>


Output:

07-10-2019 00:00:00+05:30
07-10-2019 02:30:00+08:00

Program 2:




<?php
// PHP program to illustrate DateTimeImmutable::setTimezone()
// function
    
// Creating a DateTimeImmutable() object 
$DateTimeImmutable = new DateTimeImmutable('2019-10-07'); 
    
// Getting the above datetime format 
echo $DateTimeImmutable->format('d-m-Y H:i:sP') . "\n"
    
// Calling the DateTimeImmutable::setTimezone() function
$a = $DateTimeImmutable->setTimezone(new DateTimeZone('Asia/Singapore')); 
    
// Getting a new DateTimeImmutable object
echo $a->format('d-m-Y H:i:sP'); 
?>


Output:

07-10-2019 00:00:00+00:00
07-10-2019 08:00:00+08:00

Reference:
https://devdocs.io/php/datetimeimmutable.settimezone



Last Updated : 11 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads