note 117251 added to function.time
| From: | javier | Date: | Sun, 10 May 2015 10:47:17 +0000 |
| Subject: | note 117251 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202486@lists.php.net to get a copy of this message | ||
I think the documentation regarding PHP time-related functions and time zones is a bit confusing.
Let's suppose we live in Berlin, which has time zone "Europe/Berlin" and it's
UTC+1 in winter and UTC+2 in summer. This means that for 10:00 UTC+0, the local time in Berlin is
11:00 in winter and 12:00 in summer.
The results using different methods to get the timestamp are:
<?php
ini_set('date.timezone', 'Europe/Berlin');
$time1 = date('H:i:s', time() - date('Z')); // 10:00:00
$time2 = date('H:i:s', gmdate('U')); // 12:00:00
$time3 = date('H:i:s', time()); // 12:00:00
ini_set('date.timezone', 'UTC');
$time4 = date('H:i:s', time() - date('Z')); // 10:00:00
$time5 = date('H:i:s', gmdate('U')); // 10:00:00
$time6 = date('H:i:s', time()); // 10:00:00
?>
So, if you want to ensure UTC+0 timestamp in any environment, use this:
<?php
$timestamp = time() - date('Z');
?>
The other methods, including time(), will produce different results depending on the value of
'date.timezone'. This is extremely important if you have multiple servers across different
countries, and you want to run all of them using UTC+0 without worrying about the particular time
zone in which the server is located. The alternative, of course, is calling
ini_set('date.timezone', 'UTC'); at the very beginning.
----
Server IP: 72.52.91.14
Probable Submitter: 83.34.216.80
----
Manual Page -- http://php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/117251
Del: integrated -- https://master.php.net/note/delete/117251/integrated
Del: useless -- https://master.php.net/note/delete/117251/useless
Del: bad code -- https://master.php.net/note/delete/117251/bad+code
Del: spam -- https://master.php.net/note/delete/117251/spam
Del: non-english -- https://master.php.net/note/delete/117251/non-english
Del: in docs -- https://master.php.net/note/delete/117251/in+docs
Del: other reasons-- https://master.php.net/note/delete/117251
Reject -- https://master.php.net/note/reject/117251
Search -- https://master.php.net/manage/user-notes.php