|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-08-12 12:21 UTC] remy dot fox at simbuka dot com
Description:
------------
The parsing of the date formats 'Ynd' and 'Ymd' gives a wrong result when segment in the date are not separated by any characters.
There may be similar issues with other date time format specifier combinations too, but I haven't tested them.
Test script:
---------------
$date = DateTime::createFromFormat("Ymd", "2000-10-1");
var_dump($date);
$date = DateTime::createFromFormat("Ymd", "2000101");
var_dump($date);
Expected result:
----------------
As we can see, the first $date returns false, which is correct. After all the day value was not padded with a zero. In the second example, the zero could be interpreted as either part of the month (i.e. october) or as padding for the day value. The result does not return false but it should.
object(DateTime)#2 (3) {
["date"]=>
string(26) "2000-10-01 05:18:02.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(10) "US/Pacific"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 21 23:00:01 2025 UTC |
In short: createFromFormat("Ymd", "2000101") should be rejected because it doesn't have enough digits, right? There should be four for the year, two for the month, and two for the day. The test case can be simplified: $date = DateTime::createFromFormat("d", "1"); var_dump($date); Of the other "leading zero" format specifiers, "m", "h" and "H" also parse, but "i" and "s" both fail.Your first sample fails because you didn't specify the - in your format: derick@singlemalt:~ $ php <?php $date = DateTime::createFromFormat("Y-m-d", "2000-10-1"); var_dump($date); Standard input code:3: class DateTime#1 (3) { public $date => string(26) "2000-10-01 07:55:25.000000" public $timezone_type => int(3) public $timezone => string(3) "UTC" } Works just fine here. The format characters in createFromFormat take *up* to the amount of characters acceptable for the format. So indeed, the "Y" parses 4 characters ("2000"), the "m" parses 2 characters ("10"), and the "d" up to 2 characters, but as there is only one, it parses only "1".