note 119604 added to function.checkdate
| From: | whitej71 at msu dot edu | Date: | Thu, 14 Jul 2016 17:10:07 +0000 |
| Subject: | note 119604 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206016@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 72.52.91.14
Probable Submitter: 35.10.132.83
----
Manual Page -- http://php.net/manual/en/function.checkdate.php
Edit -- https://master.php.net/note/edit/119604
Del: integrated -- https://master.php.net/note/delete/119604/integrated
Del: useless -- https://master.php.net/note/delete/119604/useless
Del: bad code -- https://master.php.net/note/delete/119604/bad+code
Del: spam -- https://master.php.net/note/delete/119604/spam
Del: non-english -- https://master.php.net/note/delete/119604/non-english
Del: in docs -- https://master.php.net/note/delete/119604/in+docs
Del: other reasons-- https://master.php.net/note/delete/119604
Reject -- https://master.php.net/note/reject/119604
Search -- https://master.php.net/manage/user-notes.php