note 129708 added to datetime.formats
| From: | Anonymous | Date: | Thu, 15 Aug 2024 20:12:31 +0000 |
| Subject: | note 129708 added to datetime.formats | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221481@lists.php.net to get a copy of this message | ||
The year-only format is a potential issue that could cause problems for you.
<?php
echo (new DateTime('1994'))->format('Y-m-d');
// Outputs today's date in the year 1984, e.g. "1994-08-15"
echo (new DateTime('2004'))->format('Y-m-d');
// Outputs today's date in the *current* year, e.g. "2024-08-15"
?>
This is because, as noted above in the documentation, 2004 gets interpreted as a time, e.g. 20:04,
rather than as a year. Even though 1994 is interpreted as a year.
One solution is to add ' T' to the end of the string:
<?php
echo (new DateTime('1994 T'))->format('Y-m-d');
// "1994-08-15"
echo (new DateTime('2004 T'))->format('Y-m-d');
// "2004-08-15"
?>
Another would be to add an arbitrary time to the beginning of the string:
<?php
echo (new DateTime('11:00 1994'))->format('Y-m-d');
// "1994-08-15"
echo (new DateTime('11:00 2004'))->format('Y-m-d');
// "2004-08-15"
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 163.116.147.31)
----
Manual Page -- https://php.net/manual/en/datetime.formats.php
Edit -- https://main.php.net/note/edit/129708
Del: integrated -- https://main.php.net/note/delete/129708/integrated
Del: useless -- https://main.php.net/note/delete/129708/useless
Del: bad code -- https://main.php.net/note/delete/129708/bad+code
Del: spam -- https://main.php.net/note/delete/129708/spam
Del: non-english -- https://main.php.net/note/delete/129708/non-english
Del: in docs -- https://main.php.net/note/delete/129708/in+docs
Del: other reasons-- https://main.php.net/note/delete/129708
Reject -- https://main.php.net/note/reject/129708
Search -- https://main.php.net/manage/user-notes.php