php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74511 Cannot construct a \DateTime from a valid format ("Y-\WW")
Submitted: 2017-04-28 17:49 UTC Modified: 2017-04-28 18:40 UTC
From: jack dot peterson at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.1.4 OS: Ubuntu 16.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jack dot peterson at gmail dot com
New email:
PHP Version: OS:

 

 [2017-04-28 17:49 UTC] jack dot peterson at gmail dot com
Description:
------------
Given the format, 'Y-\WW', one should be able to construct a date such as 2017-W01 or 2017-W1 as a valid date where W is a number from 0-52 as documented for Date.

The expected behavior here would be to create a valid date w/ the next least-significant values to be zeroed out. Current behavior is that one cannot construct a valid \DateTime from the requested format.

Test script:
---------------
 public function testCanCreateDateFromFormat() {
        $format = 'Y-\WW';
        $date = '2017-W01';

        $obj = \DateTime::createFromFormat($format, $date);
        $this->assertInstanceOf(\DateTime::class, $obj);
    }

testCanCreateDateFromFormat
Failed asserting that false is an instance of class "DateTime".

Expected result:
----------------
\DateTime::createFromFormat should return an instance of \DateTime

Actual result:
--------------
\DateTime::createFromFormat returns false in this scenario.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-04-28 17:53 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-04-28 17:53 UTC] requinix@php.net
DateTime represents an actual date. What date would 2017-W01 represent?
 [2017-04-28 18:03 UTC] heiglandreas@php.net
The formats documented for 'date' are output formats, not input formats. Accepted formats for DateTime::createFromFormat are explained at http://php.net/manual/de/datetime.createfromformat.php - I can not find a "W" in there.

Even if it would be given there, what date would you expect it to return? First Day of the week? Last Day of the Week? What is the first day of the week? Sunday? Monday? And why exactly "should (one) be able to construct a date" with that? What exactly is your use case for that?
 [2017-04-28 18:04 UTC] jack dot peterson at gmail dot com
minor correction to comment above -- 1-52 would be the expected range, not 0-52 .. .that would be more weeks than available in a given earth year :-P
 [2017-04-28 18:13 UTC] jack dot peterson at gmail dot com
Fully realizing that dates are horridly complex to deal with ... I would have 2017-W1 represent Sunday, January 1, 2017. That's a nice easy example, but I would have the \DateTime represent the least significant date possible for the requested Week.

If we're classifying this as expected behavior due to the complexity of the above calculation, then I would like to modify this report to a Documentation Request to have \Date's formats reflect which values can be used to construct a valid \DateTime and which cannot as indicated with a new column and a (*) indicating which values are acceptable inputs. The way the documentation is currently written reads along the lines of, 'if you can format a date out the you ought to be able to construct a \DateTime from the format for better or worse.
 [2017-04-28 18:40 UTC] heiglandreas@php.net
Just to clear up some missconceptions:

* The expected range would indeed be 0-52 (or 1-53) as there are years with 53 week!
* According to ISO-8601 (which is used in most countries worldwide as standard for DateTimes) the first day of the week is Monday!
* So according your example 2017-W01 would therefore be Monday the 2nd of January. Because Sunday the 1st of January is the last day of the 53rd week of 2016.

As these informations are clearly documented for the date-function[1] and the documentation for DateTime::createFromFormat [2] clearly does *not* state a 'W'-format it's neither a bug, nor a documentation issue as everything already *is* documented. And - just for clarification - there is no \Date-class in PHP.

Should you be refering to the documentation of Compound Formats [3] you might want to have a look at https://3v4l.org/M32BR which shows that the constructor DateTime already actually *does* what you want to do!

Notice something?

[1] http://php.net/manual/de/function.date.php
[2] http://php.net/manual/de/datetime.createfromformat.php
[3] http://php.net/manual/de/datetime.formats.compound.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 15 05:01:31 2024 UTC