note 127669 added to datetime.gettimestamp
| From: | rundle at rundle dot com | Date: | Mon, 26 Sep 2022 17:46:19 +0000 |
| Subject: | note 127669 added to datetime.gettimestamp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219617@lists.php.net to get a copy of this message | ||
The timestamp is always UTC and the DateTime constructor using the timestamp assumes UTC. Thus you
cannot roundtrip the timestamp without adjusting for the local time zone. Consider the following
code...
$d0 = new \DateTime();
echo json_encode($d0).'<br>';
echo 'timestamp = '.$d0->getTimestamp().'<br>';
$d1 = new \DateTime('@'.$d0->getTimestamp());
echo json_encode($d1).'<br>';
This code will produce (with server time zone set to MST)
"date":"2022-09-26
10:26:34.864244","timezone_type":3,"timezone":"America\/Phoenix"}
timestamp = 1664213194
{"date":"2022-09-26
17:26:34.000000","timezone_type":1,"timezone":"+00:00"}
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.173 (proxied: 71.228.120.105)
----
Manual Page -- https://php.net/manual/en/datetime.gettimestamp.php
Edit -- https://main.php.net/note/edit/127669
Del: integrated -- https://main.php.net/note/delete/127669/integrated
Del: useless -- https://main.php.net/note/delete/127669/useless
Del: bad code -- https://main.php.net/note/delete/127669/bad+code
Del: spam -- https://main.php.net/note/delete/127669/spam
Del: non-english -- https://main.php.net/note/delete/127669/non-english
Del: in docs -- https://main.php.net/note/delete/127669/in+docs
Del: other reasons-- https://main.php.net/note/delete/127669
Reject -- https://main.php.net/note/reject/127669
Search -- https://main.php.net/manage/user-notes.php