note 119604 deleted from function.checkdate by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 10:32:07 +0000 |
| Subject: | note 119604 deleted from function.checkdate by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210795@lists.php.net to get a copy of this message | ||
Note Submitter: whitej71 at msu dot edu
----
The below suggested date validation (from another note) does NOT work for ALL formats and date
inputs.
function validateDate($date, $format = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
A couple of examples should suffice as a warning to test carefully and particularly not to use code
that allows passing in of just any date format string:
var_dump(validateDate('7/01/16', 'n/j/y')); # false but should be true
var_dump(validateDate('7/1/16', 'm/d/y')); # false but should be true
Sorry, I don't have a suggested correction.