Tutorialspoint.dev

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:

<?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.';
}
?>

Output:

Script timezone differs from ini-set timezone.

Program 2:

<?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.';
}
?>

Output:

Script timezone differs from ini-set timezone.

Related Articles:

Reference: http://php.net/manual/en/function.date-default-timezone-set.php



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter