I have a simple code that will split the ranged dates into two. Here it is:
html:
<input type="text" name="range" />
<button type="submit>Submit</button>
php
$range = $_POST['range'];
$reservation = explode("-", $range);
$from = date('Y-m-d', strtotime($reservation[0]));
$to = date('Y-m-d', strtotime($reservation[1]));
The splitting of the date works, but whenever I put any character in the textbox and click submit, I get an undefined offset: 1
because I don't have any validation for the range dates.
Is there a way to validate ranged dates like for example if I put "10/29/2015 - 10/31/2015"
in the textbox and click submit, I will get "success
" and if I put any character like "asdasdasda
", I will get "failed
".
Any help would be much appreciated.
UPDATE
The validation if the input are in ranged date is now working, but the date validation is not. I'm using checkdate
function
$reservation = explode("-", $_POST['range']);
if (isset($reservation[0]) && isset($reservation[1])) {
$from = date('Y-m-d', strtotime($reservation[0]));
$to = date('Y-m-d', strtotime($reservation[1]));
$checkFrom = explode('-', $from);
$checkTo = explode('-', $to);
if (!checkdate($checkFrom[1], $checkFrom[2], $checkFrom[0]) || !checkdate($checkTo[1], $checkTo[2], $checkTo[0])) {
die('invaild date');
}
} else {
die('invaild date');
}