note 119325 deleted from function.date by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 10:18:49 +0000 |
| Subject: | note 119325 deleted from function.date by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210754@lists.php.net to get a copy of this message | ||
Note Submitter: Al Roker
----
When using 'U' to return a UNIX time stamp, you may not get what you expect. In the
following example, we try to get the current Unix time stamp for a user in a different timezone.
<?php
// Doesn't work
$timezone = new \DateTimeZone($userTimeZone);
$date = new \DateTime('@' . time(), $timezone);
$date->setTimezone($timezone);
$now = $date->format('U');
?>
$now will return the same (the server's current) Unix time stamp regardless which timezone your
user is in.
To get the actual Unix time stamp based on a time zone, replace format('U') as in the
following example;
<?php
// This works
$timezone = new \DateTimeZone($userTimeZone);
$date = new \DateTime('@' . time(), $timezone);
$date->setTimezone($timezone);
$now = $date->getTimestamp() + $date->getOffset();
?>