php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79926 Unclear behavior with createFromFormat using absolute and relative values
Submitted: 2020-08-03 20:23 UTC Modified: 2020-08-03 22:37 UTC
From: thomas at landauer dot at Assigned:
Status: Open Package: Date/time related
PHP Version: 7.2.32 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: thomas at landauer dot at
New email:
PHP Version: OS:

 

 [2020-08-03 20:23 UTC] thomas at landauer dot at
Description:
------------
As I learned from https://bugs.php.net/bug.php?id=67921 it is the expected behavior for an hour between 24 and 99 to just overflow to the next day:

var_dump(DateTime::createFromFormat('H:m', '25:00'));

gives me tomorrow at 01:00 - that's OK.

However, if I use one of the formats that start with 'D', the hour doesn't overflow to the next day, but to the next (e.g.) *Monday* (i.e. same weekday of the next week):



Test script:
---------------
var_dump(DateTime::createFromFormat(DateTime::RFC822, 'Mon, 3 Aug 20 25:00:00 +0000'));
var_dump(DateTime::createFromFormat(DateTime::RFC1036, 'Mon, 3 Aug 20 25:00:00 +0000'));
var_dump(DateTime::createFromFormat(DateTime::RFC1123, 'Mon, 3 Aug 20 25:00:00 +0000'));
var_dump(DateTime::createFromFormat(DateTime::RFC7231, 'Mon, 3 Aug 20 25:00:00 GMT'));
var_dump(DateTime::createFromFormat(DateTime::RFC2822, 'Mon, 3 Aug 2020 25:00:00 +0000'));
var_dump(DateTime::createFromFormat(DateTime::RSS, 'Mon, 3 Aug 2020 25:00:00 +0000'));

Expected result:
----------------
4 Aug 2020 01:00

Actual result:
--------------
10 Aug 2020 01:00

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-03 20:25 UTC] thomas at landauer dot at
My actual PHP version is 7.2.24
 [2020-08-03 21:54 UTC] requinix@php.net
-Summary: `DateTime::createFromFormat`: `H` overflows to next same *weekday* for D-format +Summary: Unclear behavior with createFromFormat using absolute and relative values -Type: Bug +Type: Documentation Problem
 [2020-08-03 21:54 UTC] requinix@php.net
Not a bug, but I'm not opposed to somehow documenting it.

1. Information given to createFromFormat does not so much specify a particular moment as it does give guidelines to the parser about what each bit in the date string means. It's like passing a string to strtotime() and explicitly saying whether each number will be interpreted as an hour or day or whatever.

2. When handling dates, absolute values are evaluated before relative values. Regardless of where they are in the string.

3. "3 Aug 20" and "25:00:00" are absolute values, "Mon" is a relative value.

See where this is going? The absolute "3 Aug 20 25:00:00" is evaluated first and it overflows to 4 Aug 01:00, then the relative "Mon" moves it to the next week.

If you adjust the absolute date back into Sunday,

// https://3v4l.org/NdcAd
var_dump(DateTime::createFromFormat('D, d M y H:i:s O', 'Mon, 2 Aug 20 25:00:00 +0000'));

then the date overflows to 3 Aug 01:00 and the "Mon" doesn't change anything.
 [2020-08-03 22:37 UTC] thomas at landauer dot at
Sorry, but to me this is a bug. Sure there's some *reason* for the current behavior (thanks for explaining the internals!).

But I bet if you ask 100 PHP programmers what the output should be, all 100 would either say `null/false` (since the input is just not valid) or "next day". Nobody would say "next week"!

So documenting it is certainly better than nothing. However, I cannot help with that, since this feels so weird/wrong to me that I can't think of a way to easily explain it ;-)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Oct 30 05:02:34 2020 UTC