php

Set timezone in PHP

The timezone value is important for any website because the date and time values are displayed based on this value. The PHP script uses the timezone value of the web server by default. This timezone value can be changed by modifying the value of the date.timezone directive in the php.ini configuration file or by adding the entry for timezone value inside the .htaccess file or using several built-in functions. Different ways of setting the timezone value in PHP have been shown in this tutorial.

Set the timezone Value by Modifying the php.ini File

One of the easiest ways to set the default timezone is by modifying the date.timezone directive inside the php.ini file. Suppose you want to set the default timezone to ‘Asia/Dhaka’. Open the php.ini file and search for the location of the date.timezone directive. Modify the line by using the following line:

date.timezone = 'Asia/Dhaka'

 
Save the file and restart the web server to set the date and time of the server based on the modified timezone value.

Set the timezone Value by Modifying the .htaccess File

Modifying the .htaccess file is another way to set the default timezone value. Open the .htaccess file and add the following line to set the default timezone value to ‘Asia/Dhaka’:

php_value date.timezone 'Australia/Melbourne'

 
Save the file and restart the web server to set the date and time of the server based on the modified timezone value.

Set the timezone Value by Using date_default_timezone_set() Function

The date_default_timezone_set() is the built-in PHP function to set the timezone value. The output of all built-in functions of PHP related to the default timezone will be changed after changing the timezone value using the date_default_timezone_set() function. The syntax of this function is given below:

Syntax

 

bool date_default_timezone_set(string timezone)

 
This function has only one mandatory argument. This argument sets a particular timezone. It returns True if the valid timezone value is passed in the argument. Otherwise, it returns False. The date_default_timezone_get() function is used to read the current timezone value of the server. So, this function can be used to check the timezone is set properly after setting the new timezone by using the date_default_timezone_set() function.

Example 1: Set the timezone by Using date_default_timezone_set() Function

Create a PHP file with the following script to set the default timezone to ‘Asia/Dhaka’ using the date_default_timezone_set() function. The date_default_timezone_get() function has been used two times to print the timezone value before and after using date_default_timezone_set() function.

<?php

//Print the current timezone
echo "The current timezone is <b>" . date_default_timezone_get() . "</b><br/>";
//Change the current timezone
date_default_timezone_set('Asia/Dhaka');
//Print the changed the timezone
echo "The current timezone is changed to <b>".  date_default_timezone_get() . "</b>";

?>

 
The following output shows that the default timezone was UTC, and the timezone has changed to Asia/Dhaka after setting the new timezone:

Set the timezone Value by Using ini_set() Function

The ini_set() is a very useful function of PHP to modify any PHP directive by using a script without accessing the php.ini file. This tutorial discussed earlier that the ‘date.timezone’ directive required to modify to change the current timezone value. So, the ini_set() function can be used to change this directive value. The syntax of this function is given below:

Syntax

 

string | false ini_set(string $option, string|int|float|bool|null $value)

 
The function’s first argument takes the directive name, and the function’s second argument takes the value. It returns a string value on success and a False on failure.

Example 2: Set the timezone by Using the ini_set() Function

Create a PHP file with the following script that will set the default timezone to ‘America/Chicago’ by using the ini_set() function. The date_default_timezone_get() function has been used two times to print the timezone value before and after using ini_set() function.

<?php

//Print the current timezone
echo "The current timezone is <b>" . date_default_timezone_get() . "</b><br/>";
//Change the current timezone
ini_set('date.timezone', 'America/Chicago');
//Print the changed timezone
echo "The current timezone is changed to <b>".  date_default_timezone_get() . "</b>";

?>

 
The following output shows that the default timezone was UTC, and the timezone has changed to ‘America/Chicago’ after setting the new timezone:

Set the timezone Value by Using DateTimeZone Class

Using DateTimeZone class is another way to change the default timezone value of the server. The uses of this class for changing the timezone have been shown in the following example:

Example 3: Set the timezone by Using DateTimeZone Class

Create a PHP file with the following script that will change the timezone value two times and print the current date and time based on the current timezone value. The display() function has been defined in the script to print the current timezone value and the current date and time based on the timezone. It has been called for the first time to show the output based on the default timezone, which is ‘UTC’. It has been called the second time to show the output based on the changed timezone, ‘Asia/Dhaka’. It has been called the third time to show the output based on the changed timezone, which is ‘Canada/Atlantic’.

<?php

//Create a date object
$date = new DateTime();

function display()
{
    global $date;
    $timezone = $date->getTimezone();
    //Print the current timezone and datetime based on timezone
    echo "The current timezone is <b>" . $timezone->getName()."</b><br/>";
    echo "The current date and time is " . $date->format('d-M-Y h:i:s') . "<br/>";

}

//Call function to print the output based on default timezone
display();

//Change the timezone to 'Asia/Dhaka'
$date->setTimezone(new DateTimeZone('Asia/Dhaka'));

//Call function to print the output based on the changed timezone
display();

//Change the timezone to 'Canada/Atlantic'
$date->setTimezone(new DateTimeZone('Canada/Atlantic'));

//Call function to print the output based on the changed timezone
display();

?>

 
The following output will appear after executing the previous script:

Conclusion

Five different ways of changing the timezone value are shown in this tutorial. If the PHP user has no permission to change the php.ini or .htaccess file, then the user can use any of the built-in functions discussed in this tutorial to change the timezone value.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.