note 117251 deleted from function.time by mazzanet
| From: | mazzanet@php.net | Date: | Sun, 27 Nov 2016 10:00:34 +0000 |
| Subject: | note 117251 deleted from function.time by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207701@lists.php.net to get a copy of this message | ||
Note Submitter: javier
----
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.