Saturday, December 28, 2024
Google search engine
HomeLanguagesPHP | date_default_timezone_set() Function

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

RELATED ARTICLES

Most Popular

Recent Comments