note 126757 added to function.date-default-timezone-set

From: Date: Tue, 11 Jan 2022 19:17:48 +0000
Subject: note 126757 added to function.date-default-timezone-set
Groups: php.notes 
Request: Send a blank email to php-notes+get-217482@lists.php.net to get a copy of this message
If we want, in runtime, to set the default timezone, we have to use date_default_timezone_set() not ini_set(). At the top of this page, the PHP states that "Instead of using date_default_timezone_set() to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone". This is very clear: default_timezone_set() is equivalent to date.timezone. This is true if you set the date.timezone directly in php.ini file: date.timezone = Europe/Athens In this case, the following functions will both return the same result: date_default_timezone_get(); ini_get('date.timezone'); The result is 'Europe/Athens'. And this can also be verified by running phpinfo(); function. But many users don't have access to php.ini file, or they are running multiple PHP projects, with different needs. How can they affect the default timezone of time functions? If you haven't set the timezone in run php.ini and run the phpinfo(); function, you get two different values: 1) Default timezone (by default this is 'UTC') 2) date.timezone (by default this is empty if not set directly in php.ini) In the documentation of date_default_timezone_get(), PHP states that: In order of preference, this function returns the default timezone by: 1) Reading the timezone set using the date_default_timezone_set() function (if any) 2) Reading the value of the date.timezone ini option (if set) We can test the above, by running the following script and setting those variables to have different values. In the final step, we see that the timezone that affects date functions comes from date_default_timezone_set(), not from ini_set(). According to the example, the Paris timezone is in effect, not that of Athens. This means that, when both values are set, only date_default_timezone_set() affects the date functions used in our scripts. Note: The following example has been tested on Linux + PHP 7.4.27 and Windows + PHP 8.1.1. <?php // What are the initial values (as set in php.ini)? $default_timezone = date_default_timezone_get(); $date_timezone = ini_get('date.timezone'); echo "INITIAL VALUES ARE:\r\n"; echo "default_timezone = '$default_timezone'\r\n"; echo "date_timezone = '$date_timezone'\r\n\r\n"; // Set new values date_default_timezone_set('Europe/Paris'); // Paris has a time offset of +1 (UTC+1) ini_set('date.timezone', 'Europe/Athens'); // Athens has a time offset of +2 (UTC+2) $default_timezone = date_default_timezone_get(); $date_timezone = ini_get('date.timezone'); echo "NEW VALUES ARE:\r\n"; echo "default_timezone = '$default_timezone'\r\n"; echo "date_timezone = '$date_timezone'\r\n\r\n"; // What timezone is taken into account in the time functions? $offset = date_offset_get(new DateTime) / 3600; $date = date('l jS \of F Y h:i:s A'); echo "Effective time offset is '$offset'\r\n"; echo "Current date/time is '$date'\r\n"; ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 79.166.113.147) ---- Manual Page -- https://php.net/manual/en/function.date-default-timezone-set.php Edit -- https://main.php.net/note/edit/126757 Del: integrated -- https://main.php.net/note/delete/126757/integrated Del: useless -- https://main.php.net/note/delete/126757/useless Del: bad code -- https://main.php.net/note/delete/126757/bad+code Del: spam -- https://main.php.net/note/delete/126757/spam Del: non-english -- https://main.php.net/note/delete/126757/non-english Del: in docs -- https://main.php.net/note/delete/126757/in+docs Del: other reasons-- https://main.php.net/note/delete/126757 Reject -- https://main.php.net/note/reject/126757 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217482) next »