Bug->Doc #79926 [Opn]: Unclear behavior with createFromFormat using absolute and relative values
| From: | requinix@php.net | Date: | Mon, 03 Aug 2020 21:54:33 +0000 |
| Subject: | Bug->Doc #79926 [Opn]: Unclear behavior with createFromFormat using absolute and relative values | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-17745@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79926&edit=1
ID: 79926
Updated by: requinix@php.net
Reported by: thomas at landauer dot at
-Summary:
DateTime::createFromFormat: H overflows to next
same *weekday* for D-format
+Summary: Unclear behavior with createFromFormat using
absolute and relative values
Status: Open
-Type: Bug
+Type: Documentation Problem
Package: Date/time related
Operating System: Linux
PHP Version: 7.2.32
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2020-08-03 20:25:17] thomas at landauer dot at
My actual PHP version is 7.2.24
------------------------------------------------------------------------
[2020-08-03 20:23:12] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79926&edit=1