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!??

5
  • What does $date look like? And how is that line ($date = $datetime->createFromFormat('Y-m-d', $date_test);) doing anything if you're not using $date? Commented Sep 29, 2013 at 7:06
  • 10=October having 31 days is still valid. Commented Sep 29, 2013 at 7:07
  • As @Corrupt answered, 13 is translated as 13AD(Anno Domini). So very much of valid dates. Commented Sep 29, 2013 at 7:09
  • 1
    @Lenin Might I add, the year 13 is valid as well, although most of the people that lived that year have already died. Commented Sep 29, 2013 at 7:09
  • 3
    'DateTime::createFromFormat()' is a static function. Commented Sep 29, 2013 at 7:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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. Strange
Your test is calling createFromFormat() in a non-static way.
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

1 Comment

@lauthiamkok Of course it doesn't! As I clearly stated in my answer 13-10-31 is a valid date.
0

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

3 Comments

Please check here for complete date formats php.net/manual/en/function.date.php
Did you expect years to be of leading zero for less than 4digits? @lauthiamkok
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.