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: 2017-02-12 20:33 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: Verified Package: *Directory/Filesystem functions
PHP Version: 5.6.23 OS: ubuntu/windows
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Add a Patch

Pull Requests

Add a Pull Request

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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Oct 19 15:01:27 2019 UTC