php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72586 wrong createfromformat for leap year
Submitted: 2016-07-12 16:09 UTC Modified: 2020-12-07 16:45 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: stierlitz122 at gmail dot com Assigned:
Status: Closed Package: Date/time related
PHP Version: 5.6.23 OS: ubuntu/windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: stierlitz122 at gmail dot com
New email:
PHP Version: OS:

 

 [2016-07-12 16:09 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-12 19:02 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2016-07-12 19:02 UTC] cmb@php.net
Confirmed: <https://3v4l.org/ei8A2>.
 [2017-02-12 20:33 UTC] derick@php.net
-Type: Bug +Type: Documentation Problem -Package: Date/time related +Package: *Directory/Filesystem functions
 [2017-02-12 20:33 UTC] derick@php.net
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.
 [2020-12-07 16:45 UTC] cmb@php.net
-Package: *Directory/Filesystem functions +Package: Date/time related
 [2022-06-02 14:29 UTC] git@php.net
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
 [2022-06-02 14:29 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC