2

Why php DateTime object does not give me errors for an invalid date input? For instance,

$date_test = '13-10-31';

$datetime = new DateTime();
$date = $datetime->createFromFormat('Y-m-d', $date_test);
$date_errors = $datetime->getLastErrors();
print_r($date_errors);

result,

Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

)

I have set the date format to be 'Y-m-d' which is yyyy-mm-dd so '13-10-31' should be an error input isn't?

EDIT

if I change this line $datetime->createFromFormat('Y-m-d', $date_test); to

$datetime->createFromFormat('YY-m-d', $date_test);

I will get an error no matter what I input. For instance,

$date_test = '2013-10-31';

result,

Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 2
    [errors] => Array
        (
            [4] => Unexpected data found.
            [10] => Data missing
        )

)

Why!??

Run
  • 54,938
  • 169
  • 450
  • 748

3 Answers3

5

echo $date->format('d-m-Y') showed 31-10-0013, so looks like php threating your date as normal.


UPDv1:

This is not mentioned in docs. Still if you look into echo $date->format('U'); it will give you a negative timestamp.

Since PHPv5.3.0 this feature is confirmed, according to 3v4l test.

BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • 1
    It's interesting that on my windows system `$date->getTimestamp()` returns false, while `$date->format('U')` gives the expected negative timestamp. It works fine on my linux box though and [here](http://3v4l.org/EKp1a). Strange – vascowhite Sep 29 '13 at 07:33
  • Your test is calling `createFromFormat()` in a non-static way. – vascowhite Sep 29 '13 at 07:36
2

Your date is a valid date, however your code is incorrect in that DateTime::createFromFormat() is a static function as is getLastErrors(). Your example code should be:-

$date_test = '13-10-31';
$date = \DateTime::createFromFormat('Y-m-d', $date_test);
$date_errors = \DateTime::getLastErrors();
print_r($date_errors);

However, there will still be no errors as the 31st October 13 (13-10-31) is a valid date. When you provide '13' as the year, DateTime casts it to an integer, 13, therefore, it is a valid year.

If you want to discard double digit years as invalid, then you will have to write your own validation functions.

I would recommend a thorough read of the PHP Date and Time extensions documentation

vascowhite
  • 18,120
  • 9
  • 61
  • 77
0

this is not because of the date inputted but by the incorrect defination of Year parameter.

Sunil Verma
  • 2,490
  • 1
  • 14
  • 15
  • 1
    Please check here for complete date formats http://php.net/manual/en/function.date.php – Sunil Verma Sep 29 '13 at 07:22
  • Did you expect years to be of leading zero for less than 4digits? @lauthiamkok – Lenin Sep 29 '13 at 07:24
  • i was commenting about your second edit. i.e $datetime->createFromFormat('YY-m-d', $date_test); I will get an error no matter what I input. For instance, in case you thought it otherwise. – Sunil Verma Sep 29 '13 at 07:27