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. Show
Set the timezone Value by Modifying the php.ini FileOne 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' Set the timezone Value by Modifying the .htaccess FileModifying 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' Set the timezone Value by Using date_default_timezone_set() FunctionThe 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) Example 1: Set the timezone by Using date_default_timezone_set() FunctionCreate 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 Set the timezone Value by Using ini_set() FunctionThe 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) Example 2: Set the timezone by Using the ini_set() FunctionCreate 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 Set the timezone Value by Using DateTimeZone ClassUsing 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 ClassCreate 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 ConclusionFive 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. How to set the time zone in PHP?The date_default_timezone_set() function sets the default timezone used by all date/time functions in the script.
How to set UTC time in PHP?date_default_timezone_set() sets the default timezone used by all date/time functions. Instead of using this function to set the default timezone in your script, you can also use the INI setting date. timezone to set the default timezone.
What is PHP default timezone?The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts.
How to set timezone in PHP Ubuntu?You can also set the timezone for a single PHP script by defining timezone inside the PHP script. Simply edit your PHP script and add date_default_timezone_set(“XXX”) line at top of script. Change XXX with your preferred timezone. date_default_timezone_set("America/New_York"
|