Doc #72586 [Ver->Csd]: wrong createfromformat for leap year
[email protected] Thu, 02 Jun 2022 14:29:57 +0000
| Newsgroups | php.doc.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=72586&edit=1 ID: 72586 Updated by: [email protected] Reported by: stierlitz122 at gmail dot com Summary: wrong createfromformat for leap year -Status: Verified +Status: Closed Type: Documentation Problem Package: Date/time related Operating System: ubuntu/windows PHP Version: 5.6.23 Block user comment: N Private report: N New Comment: Automatic comment on behalf of derickr Revision: https://github.com/php/doc-en/commit/33dd071b0dc703cb8b70329ab46ccc8820b14280 Log: Fixed bug #72586: Document that order of characters is important Previous Comments: ------------------------------------------------------------------------ [2017-02-12 20:33:40] [email protected] createFromFormat parses data from left to right, turning them into Y-m-d (& H:i:s) values. In this case, when it sees the "59" it assumes the year is 2017 (the current year) (March 1st). When it is than re-expressed for 2016, the value becomes 60 (as March 1st is the 60th day in a leap year). As 29-02-2016 and 2016-02-29 always means the same, there is no discrepancy here. The documentation isn't quite clear on this parsing order, but it hints at that through the ! and + modifiers. So I am marking this as a documentation issue. ------------------------------------------------------------------------ [2016-07-12 19:02:22] [email protected] Confirmed: <https://3v4l.org/ei8A2>. ------------------------------------------------------------------------ [2016-07-12 16:09:14] stierlitz122 at gmail dot com Description: ------------ When using day of the year character 'z' with 'Y' for creation date - order is important, but not important when using 'd-m-Y'. --- From manual page: http://www.php.net/datetime.createfromformat --- Test script: --------------- <?php $date1 = DateTime::createFromFormat('z Y', "59 2016"); //wrong with leap year $date2 = DateTime::createFromFormat('Y z', "2016 59"); //correct $date3 = DateTime::createFromFormat('d-m-Y', "29-02-2016"); //correct $date4 = DateTime::createFromFormat('Y-m-d', "2016-02-29"); //correct // let's see: print $date1->format("z Y")."\n"; //60 2016 print $date2->format("z Y")."\n"; //59 2016 print $date3->format("z Y")."\n"; //59 2016 print $date4->format("z Y")."\n"; //59 2016 Expected result: ---------------- 59 2016 59 2016 59 2016 59 2016 Actual result: -------------- 60 2016 59 2016 59 2016 59 2016 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72586&edit=1