Doc #79926 [Opn->Csd]: Unclear behavior with createFromFormat using absolute and relative values

From: Date: Sat, 04 Jun 2022 17:14:29 +0000
Subject: Doc #79926 [Opn->Csd]: Unclear behavior with createFromFormat using absolute and relative values
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19466@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:         git@php.net
 Reported by:        thomas at landauer dot at
 Summary:            Unclear behavior with createFromFormat using
                     absolute and relative values
-Status:             Open
+Status:             Closed
 Type:               Documentation Problem
 Package:            Date/time related
 Operating System:   Linux
 PHP Version:        7.2.32
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of derickr
Revision: https://github.com/php/doc-en/commit/dab41534c60aee794bfba84e22d5dfef343cb38f
Log: Fixed bug #79926: Unclear behavior with createFromFormat using absolute and relative values


Previous Comments:
------------------------------------------------------------------------
[2022-05-13 14:21:06] derick@php.net

requinix@php.net — your explanation is spot on. Where and how would you suggest we document
this?

------------------------------------------------------------------------
[2020-08-03 22:37:18] 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 ;-)

------------------------------------------------------------------------
[2020-08-03 21:54:33] 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 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


Thread (5 messages)

« previous php.doc.bugs (#19466) next »