note 129052 added to datetime.format
| From: | reed at taeluf dot com | Date: | Fri, 17 Nov 2023 21:49:51 +0000 |
| Subject: | note 129052 added to datetime.format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221006@lists.php.net to get a copy of this message | ||
Formatting for MySQL:
<?php
// as your application needs
$timezone = new \DateTimeZone("Europe/Tallinn");
$dt = new \DateTime('now', $timezone);
// MySql does not store timezone information, so normalize it
$dt->setTimezone(new \DateTimeZone("UTC"));
$db_friendly_string = $dt->format('Y-m-d H:i:s');
// insert $db_friendly_string into a DATETIME mysql column
?>
Loading FROM mysql is essentially the same:
<?php
$timezone = new \DateTimeZone("Europe/Tallinn");
$db_datetime = '2023-11-17 15:19:03';
$dt = \DateTime::createFromFormat('Y-m-d H:i:s', $db_datetime);
$dt->setTimezone($timezone);
?>
Also see https://dev.mysql.com/doc/refman/8.0/en/datetime.html
MySql docs state "In MySQL 8.0.19 and later, you can specify a time zone offset when inserting
a TIMESTAMP or DATETIME value into a table. See Section 9.1.3, âDate and Time Literalsâ,
for more information and examples. "
However, in my testing on mysql Ver 8.0.35, the time zone offset is discarded & the datetime
string that's stored is exactly as you send to server - not modified with the offset.
If you insert with an offset, this is the format:
'2023-11-17 15:19:03-08:00'
Or the DateTime Format:
'Y-m-d H:i:sP'
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 2600:1702:42e0:bf10::1c)
----
Manual Page -- https://php.net/manual/en/datetime.format.php
Edit -- https://main.php.net/note/edit/129052
Del: integrated -- https://main.php.net/note/delete/129052/integrated
Del: useless -- https://main.php.net/note/delete/129052/useless
Del: bad code -- https://main.php.net/note/delete/129052/bad+code
Del: spam -- https://main.php.net/note/delete/129052/spam
Del: non-english -- https://main.php.net/note/delete/129052/non-english
Del: in docs -- https://main.php.net/note/delete/129052/in+docs
Del: other reasons-- https://main.php.net/note/delete/129052
Reject -- https://main.php.net/note/reject/129052
Search -- https://main.php.net/manage/user-notes.php