php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44453 DateTime::__construct() does not detect illegal Dates
Submitted: 2008-03-17 10:59 UTC Modified: 2008-07-18 07:24 UTC
From: martin dot contento at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.5 OS: Linux
Private report: No CVE-ID: None
 [2008-03-17 10:59 UTC] martin dot contento at gmail dot com
Description:
------------
As i can not comment on http://bugs.php.net/bug.php?id=42971 I opened this new bug, i hope this is ok...

The error here is that an invalid Date like this one should be caught in the DateTime's constructor, which then should return false as it claims in it's documentation (http://de2.php.net/manual/en/function.date-create.php)

BTW: This specific string gains it's "importance" by the fact that it's the default mysql puts in a DATETIME field.

Reproduce code:
---------------
<?php
error_reporting(E_ALL | E_NOTICE | E_STRICT);
date_default_timezone_set('Europe/Berlin');

$buggy1 = '0000-00-00 00:00:00';
print "Creating DateTime with $buggy1\n";
$dt = new DateTime($buggy1);

if ( ($buggy1 instanceof bool) and ($buggy1 === false)) {
  print "hooray, it correctly noticed the error\n";
} else {
  print "oh no, it did ... something...\n";
  print "formated as Y-m-d H:i:s\n";
  var_dump($dt->format('Y-m-d H:i:s'));
  print "formated as U\n";
  var_dump($dt->format('U'));
}
?>


Expected result:
----------------
Creating DateTime with 0000-00-00 00:00:00
hooray, it correctly noticed the error

Actual result:
--------------
Creating DateTime with 0000-00-00 00:00:00
oh no, it did ... something...
formated as Y-m-d H:i:s
string(19) "1999-11-30 00:00:00"
formated as U
string(9) "943916400"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-17 11:07 UTC] martin dot contento at gmail dot com
I'm very sorry, i mad a mistake when copying the code from my testfile, it should be like this (the if needs to check "$dt1" and not "$buggy1")


Reproduce code:
---------------
<?php
error_reporting(E_ALL | E_NOTICE | E_STRICT);
date_default_timezone_set('Europe/Berlin');

$buggy1 = '0000-00-00 00:00:00';
print "Creating DateTime with $buggy1\n";
$dt1 = new DateTime($buggy1);

if ( ($dt1 instanceof bool) and ($dt1 === false)) {
  print "hooray, it correctly noticed the error\n";
} else {
  print "oh no, it did ... something...\n";
  print "formated as Y-m-d H:i:s\n";
  var_dump($dt1->format('Y-m-d H:i:s'));
  print "formated as U\n";
  var_dump($dt1->format('U'));
}
?>
 [2008-07-18 07:24 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


This is not a bug. MySQL is being silly to use this string as a default value. In PHP 5.3 you can now detect this however, by using date_parse_from_format( "Y-m-d H:i:s"); and then check the contents of date_get_last_errors().
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 23:01:30 2024 UTC