note 119325 added to function.date
| From: | AlRoker | Date: | Fri, 13 May 2016 02:18:56 +0000 |
| Subject: | note 119325 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205478@lists.php.net to get a copy of this message | ||
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();
?>
----
Server IP: 72.52.91.14
Probable Submitter: 31.50.141.245
----
Manual Page -- http://php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/119325
Del: integrated -- https://master.php.net/note/delete/119325/integrated
Del: useless -- https://master.php.net/note/delete/119325/useless
Del: bad code -- https://master.php.net/note/delete/119325/bad+code
Del: spam -- https://master.php.net/note/delete/119325/spam
Del: non-english -- https://master.php.net/note/delete/119325/non-english
Del: in docs -- https://master.php.net/note/delete/119325/in+docs
Del: other reasons-- https://master.php.net/note/delete/119325
Reject -- https://master.php.net/note/reject/119325
Search -- https://master.php.net/manage/user-notes.php