php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66201 DateTime::createFromFormat returns DateTime object although the date is invalid
Submitted: 2013-11-29 11:02 UTC Modified: 2013-11-29 11:37 UTC
From: ante dot drnasin at wirecard dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.5.6 OS: Win7 Pro x64
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ante dot drnasin at wirecard dot com
New email:
PHP Version: OS:

 

 [2013-11-29 11:02 UTC] 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!?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-11-29 11:37 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2013-11-29 11:37 UTC] derick@php.net
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*).
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 21:00:02 2026 UTC