I'm trying to check the validity of a date before it's inserted into a MySQL db.
The date is submitted by a form in the format MM-DD-YYYY
This works:
$month = substr($date,0,2);
$day = substr($date,3,2);
$year = substr($date,6,4);
if (!checkdate($month, $day, $year)) {
$error = "You have entered an invalid date.";
echo $error;
}
But this doesn't - though it seems like it should. I get the error message "Wrong parameter count for checkdate()". Note that $cdate echoes properly - "MM, DD, YYYY" (without quotes)
$cdate = preg_replace("/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})$/", '\1, \2, \3', $date);
echo $cdate;
if (!checkdate($cdate)) {
$error = "You have entered an invalid date.";
echo $error;
}
It seems like checkdate() is getting fed the same data either way, but PHP doesn't agree. Any thoughts?