Bug #66201 [Opn->Nab]: DateTime::createFromFormat returns DateTime object although the date is invalid

From: Date: Fri, 29 Nov 2013 11:37:58 +0000
Subject: Bug #66201 [Opn->Nab]: DateTime::createFromFormat returns DateTime object although the date is invalid
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183008@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66201&edit=1 ID: 66201 Updated by: derick@php.net Reported by: ante dot drnasin at wirecard dot com Summary: DateTime::createFromFormat returns DateTime object although the date is invalid -Status: Open +Status: Not a bug Type: Bug Package: Date/time related Operating System: Win7 Pro x64 PHP Version: 5.5.6 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php The reason why this works like it does because exactly the same behaviour happens when doing this: $d = new DateTime("31.02.2013"); PHP's date/time stuff always allows larger numbers, and then overflows - but only if the pattern itself is correct (with DateTime).There are never going to be 31 one days in February, but the *pattern* matches (days are 0-31, months are 0-12 and years, well, are years). If you specify your own format, the only thing that "d" really says is: give me a number that is going to be interpreted as days with two characters maximum. "99" is also a string that will match that. And of course, the same is through for "m" and "Y". The warning shows up because we've added an extra check, but we didn't suddenly want things to break (which would happen if this was made an *error*). Previous Comments: ------------------------------------------------------------------------ [2013-11-29 11:02:59] ante dot drnasin at wirecard dot com Description: ------------ As per manual DateTime::createFromFormat returns DateTime object or false. Test script: --------------- $sDate = "25.27.2013"; //d.m.Y format $mDateTime = DateTime::createFromFormat("d.m.Y", $sDate); if($mDateTime instanceof DateTime) { print("Date is ok!"); } else { print("Date not ok!"); } var_dump($mDateTime); var_dump(DateTime::getLastErrors()); Expected result: ---------------- Date not ok! $mDateTime = false; DateTime::getLastErrors() holds "The parsed date was invalid" message Actual result: -------------- Date is ok. (converted to 2015-03-25 10:57:26 which is weird but ok. can swallow that although it doesn't make any sense to calculate a "real" date from 25.27.2013 input to begin with) $mDateTime is DateTime object BUT! DateTime::getLastErrors() is: array(4) { ["warning_count"]=> int(1) ["warnings"]=> array(1) { [10]=> string(27) "The parsed date was invalid" } ["error_count"]=> int(0) ["errors"]=> array(0) { } } so there are erros in error array but the date is valid and is DateTime object instead of false!? ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66201&edit=1

« previous php.bugs (#183008) next »