I have an input date field:
<input type="date" id="date" name="date" />
By default if the user didn't enter a date, It contains "mm/dd/yyyy".
I want to check that the sent data is a valid date:
//Assign the posted date to a variable.
$date = $_POST['date'];
//Explode the date to 3 parts 'mm', 'dd', 'yyyy'
$test_arr = explode('/', $date);
//Check if $date is not valid date 'mm/dd/yyyy' [note the ! before each condition]
if ( !(checkdate($test_arr[0], $test_arr[1], $test_arr[2])) && !(preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/", $date)) )
{
echo "Please enter a valid date";
}
If the user submit without entering a date, Then I get an empty variable.
If the user changed the input and entered characters or digits, Or empty, I get the following errors:
Notice: Undefined offset: 1.
Notice: Undefined offset: 2.
checkdate() expects parameter 1 to be long, string given.
I could check if it's not empty, then continue to the conditions, But if I get data like: ('123', 'ijij', 'ioh3ihi33', 'bla bla' ..), I would get the same errors.
The conditions should print a message in case of invalid date.
So how to do that?