php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60650 DateTime validation is incomplete
Submitted: 2012-01-04 10:15 UTC Modified: 2012-01-04 14:20 UTC
From: ville dot salonen at iki dot fi Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.3.8 OS:
Private report: No CVE-ID: None
 [2012-01-04 10:15 UTC] ville dot salonen at iki dot fi
Description:
------------
DateTime validation is incomplete. According to DateTime::__construct 
documentation this is not a bug:

// Non-existent values roll over.
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";

I disagree. There should be at least some way to require a proper validation (such 
as checkdate() function provides) for input dates. If you think this is incorrect 
behavior and rolling over is the desired one, then please implement the rolling so 
that one can for example give "2000-02-37" as the argument. Now DateTime checks 
the day of the month like this: 0 <= x <= 31.

Test script:
---------------
<?php
$date = new DateTime('2011-02-31');
print $date->format('Y-m-d');

$date = new DateTime('2011-02-32');
print $date->format('Y-m-d');
?>


Expected result:
----------------
Both DateTime constructors should throw an Exception describing that the date is 
invalid.

Actual result:
--------------
Now the constructor with '2011-02-32' as its parameter is the only one throwing 
Exception about invalid date.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-04 14:20 UTC] derick@php.net
-Status: Open +Status: Bogus
 [2012-01-04 14:20 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

Hi!

The parser checks for possible day numbers (00-31) and the validation for validity. With date_get_last_errors() you can find the warnings:

derick@whisky:~$ php
<?php
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
var_dump(date_get_last_errors());
?>
2000-03-01 00:00:00+00:00
array(4) {
  'warning_count' =>
  int(1)
  'warnings' =>
  array(1) {
    [11] =>
    string(27) "The parsed date was invalid"
  }
  'error_count' =>
  int(0)
  'errors' =>
  array(0) {
  }
}

We can't really change this because of BC reasons.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 16 22:01:30 2024 UTC