PHP | date_default_timezone_set() Function

The date_default_timezone_set() function is an inbuilt function in PHP which is used to set the default timezone used by all date/time functions in a script. This function returns False if the timezone is not valid, or True otherwise.

Syntax:

bool date_default_timezone_set( $timezone_identifier )

Parameters: This function accepts single parameter $timezone_identifier which is mandatory. This parameter set the timezone identifier, like UTC or Asia/Kolkata.



Return Value: This function returns False if the timezone_identifier is not valid, or True otherwise.

Below programs illustrate the date_default_timezone_set() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Set timezone
date_default_timezone_set('Asia/Kolkata');
  
// Create 
$timezone_object = date_default_timezone_get();
  
// Compare the timezone with ini-set timezone
if (strcmp($timezone_object, ini_get('date.timezone'))){
    echo 'Script timezone differs from ini-set timezone.';
} else {
    echo 'Script timezone and ini-set timezone match.';
}
?>

chevron_right


Output:

Script timezone differs from ini-set timezone.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Set the timezone
date_default_timezone_set('Asia/Dubai');
  
// Create the timezone object
$timezone_object = date_default_timezone_get();
  
// Compare the timezone with ini-set timezone
if (strcmp($timezone_object, ini_get('date.timezone'))){
    echo 'Script timezone differs from ini-set timezone.';
} else {
    echo 'Script timezone and ini-set timezone match.';
}
?>

chevron_right


Output:

Script timezone differs from ini-set timezone.

Related Articles:

Reference: http://php.net/manual/en/function.date-default-timezone-set.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.