PHP set timezone
In this post, you will learn about the PHP set timezone.
PHP provides the date_default_timezone_set() in-built function 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 )
Here, the $timezone_identifier sets the timezone identifier, like UTC or Asia/Kolkata. It accepts only the single parameter which is required.
Set the timezone in php.ini file
We can easily change the default timezone in PHP. For this, open the 'php.ini' file and find the date section like the following.
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone ="UTC"
Here, uncomment the date.timezone and set its value to the timezone you need. Here, we have changed the default PHP timezone to 'Asia/Calcutta'. Now, save and exit the 'php.ini' file and restart the server. The timezone settings should now be modified. This is the timezone used by all PHP date/time functions in your scripts.
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Calcutta"
You can get a list of all the valid timezones from here - http://www.php.net/manual/en/timezones.php
PHP set timezone in procedural way
We can also set the timezone by changing it in the code without touching the ini file. At the beginning of your code, add the following code and set the timezome. This is the procedural style to set the timezone.
<?php
date_default_timezone_set('America/Los_Angeles');
echo date_default_timezone_get();
?>
Output of the above code:
America/Los_Angeles
PHP set timezone in object-oriented style
We can also set the time zone for the DateTime object in an object-oriented way. Here is the syntax -
public DateTime::setTimezone(DateTimeZone $timezone): DateTime
The following example demonstrates how to set the timezone in object-oriented way in PHP code.
<?php
$date = new DateTime('2020-05-09', new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:sP')."<br/>";
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:sP')."<br/>";
?>
Output of the above code:
2020-05-09 00:00:00-04:00
2020-05-09 05:00:00+01:00
PHP check for timezone
We can compare the set timezone with the ini set timezone.
<?php
// Set timezone
date_default_timezone_set('Asia/Kolkata');
// get created timezone
$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 of the above code:
Script timezone differs from ini-set timezone.
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP random quote generator
PHP calculate percentage of total
PHP sanitize input for MySQL
Display PDF using an AJAX call
How to fetch data from database in php and display in pdf
How to read CSV file in PHP and store in MySQL
How to create a doc file using PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Sending HTML form data to an email address