PHP check user input date
date, php, validation
Solution
You can use a combination of `strtotime()` and `checkdate()` to see if the date is valid:
function isRealDate($date) {
if (false === strtotime($date)) {
return false;
}
list($year, $month, $day) = explode('-', $date);
return checkdate($month, $day, $year);
}
usage
if (isRealDate($geboortedatum)) {
// date is ok
}
else {
// date is not ok
}
Problem
So, a user can input a birth date by typing it or using a date picker. This could allow mistakes, so on the server side I'd like to check if it's a valid date. I read a lot on strtotime() and saw a lot of examples, but not a 100% correct one. This is what I have: ``` $geboortedatum = $_POST['geboortedatum']; $geboortedatum = date("Y-m-d", strtotime($geboortedatum)); list($y, $m, $d) = explode("-", $geboortedatum); // var_dump(checkdate($d,$m,$y)); if(checkdate($d, $m, $y)) { echo "OK Date"; } else { echo "BAD Date"; exit(); } ``` This works most of the time. Is there any solid method to check if the input is a real and correct date? // EDIT That was not a good example! It also doesn't work when the user inputs 31-31-1980. This is also valid, although it shouldn't be!