PHP | IntlCalendar setTimeZone() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.