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

From: Date: Mon, 27 Dec 2021 07:51:10 +0000
Subject: Doc #79685 [Opn->Dup]: createFromFormat doesn't work easily with unpadded components
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19374@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79685&edit=1

 ID:                 79685
 Updated by:         requinix@php.net
 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] requinix@php.net

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


Thread (2 messages)

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