Open In App

PHP | IntlCalendar setTimeZone() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The IntlCalendar::setTimeZone() function is an inbuilt function in PHP which is used to set the new timezone for this calendar. The time is represented in terms of object and it preserves to the detriment of the timezone field values.

Syntax:

  • Object oriented style
    bool IntlCalendar::setTimeZone( mixed $timeZone )
  • Procedural style
    bool intlcal_set_time_zone( IntlCalendar $cal, mixed $timeZone )

Parameters: This function uses two parameters as mentioned above and described below:

  • $cal: This parameter holds the resource of IntlCalendar.
  • $timeZone: This parameter holds the new timezone which is used by this calendar.
    • NULL: The default timezone will be used.
    • IntlTimeZone: It is used directly.
    • DateTimeZone: The identifier of DateTimeZone object will be extracted and an ICU timezone object will be created.
    • string: It should be a valid ICU timezone identifier.

Return Value: This function returns TRUE on success and FALSE on failure.

Below program illustrates the IntlCalendar::setTimeZone() function in PHP:

Program:




<?php
  
// Set the date timezone
ini_set('date.timezone', 'Asia/Calcutta');
  
// Create a DateTime object
$calendar = IntlCalendar::fromDateTime('2019-03-21 09:19:29');
  
// Format the DateTime object 
echo IntlDateFormatter::formatObject($calendar, IntlDateFormatter::FULL), "\n";
  
// Create new IntlGregorianCalendar object
$calendar->setTimezone(new DateTimeZone('Asia/Singapore')); 
  
// Format the DateTime object 
echo IntlDateFormatter::formatObject($calendar, IntlDateFormatter::FULL), "\n";
  
// Set the timezone
$calendar->setTimeZone('GMT+05:30');
  
// Format the DateTime object 
echo IntlDateFormatter::formatObject($calendar, IntlDateFormatter::FULL), "\n";
  
// Set the timezone
$calendar->setTimeZone(IntlTimeZone::getGMT());
  
// Format the DateTime object 
echo IntlDateFormatter::formatObject($calendar, IntlDateFormatter::FULL);
  
?>


Output:

Thursday, March 21, 2019 at 9:19:29 AM India Standard Time
Thursday, March 21, 2019 at 11:49:29 AM Singapore Standard Time
Thursday, March 21, 2019 at 9:19:29 AM GMT+05:30
Thursday, March 21, 2019 at 3:49:29 AM GMT

Reference: https://www.php.net/manual/en/intlcalendar.settimezone.php


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