Doc #79685 [Opn->Dup]: createFromFormat doesn't work easily with unpadded components

[email protected] Mon, 27 Dec 2021 07:51:10 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=79685&edit=1

 ID:                 79685
 Updated by:         [email protected]
 Reported by:        contact at eslistavenga dot nl
 Summary:            createFromFormat doesn't work easily with unpadded
                     components
-Status:             Open
+Status:             Duplicate
 Type:               Documentation Problem
 Package:            Date/time related
 Operating System:   Linux
 PHP Version:        7.4.6
 Block user comment: N
 Private report:     N

 New Comment:

Closing in favor of https://github.com/php/php-src/issues/7834


Previous Comments:
------------------------------------------------------------------------
[2020-06-10 11:21:08] [email protected]

The docs should explain in a little more detail about how the parser tries to handle this sort of situation. As when happens with other date strings in PHP, 'G' will not match *only* 0-23 but will support anything up to 99 and then overflow the date if you go too high. When the docs say 1 or 2 digits, that means it will consume one digit at a minimum and then consume the next if it's a digit as well. It will *not* stop at one digit just because the digit was 3-9, *nor* will it backtrack if it matches '2' then 4-9.
So 'G' is taking '71' (which would result in overflow), thus leaving the 's' one digit short, causing the parsing to fail.

Moral of the story is that you should always use padded numbers in your computer-friendly date strings.

------------------------------------------------------------------------
[2020-06-10 09:02:46] contact at eslistavenga dot nl

Description:
------------
When using the DateTime format ymdGis without any formatting, like hour/minute/second separator, the method will throw an error. See code example below. The method expects a leading 0, even tho the hour format 'G' was used and not 'H'

Test script:
---------------
<?php

//won't work
$date = DateTime::createFromFormat('ymdGis', '20061071018');

var_dump($date); //false
var_dump(DateTime::getLastErrors());

//will work
$date = DateTime::createFromFormat('ymdG-is', '2006107-1018');

var_dump($date); //DateTime::object
var_dump(DateTime::getLastErrors());

Expected result:
----------------
A valid DateTime object

Actual result:
--------------
false


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79685&edit=1