0

I wrote a simple piece of code to validate the format of a date. The format of the date in my case is d/m/y

List of tests that I run successfully

10/12/2019 DATE OK

aa/12/2019 DATE KO

10-12-2019 DATE KO

But there is this case that surprises me:

32/12/2019 DATE OK

Why does this happen? Do I need to add controls on the range of days and months?

$value = '32-12-2019';
$checkDate = \DateTime::createFromFormat('d/m/Y', $value);
if ($checkDate) {
    print("DATE OK");
} else {
    print("DATE KO");
}

1 Answers1

0

I'm using

public static function validateDate($date, $format = 'Y-m-d')
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) === $date;
}

It validates correctness of date and required format

PeliCan
  • 196
  • 1
  • 1
  • 8