Just realize that the date validation is little tricky, yesterday I just make my new date validation method. Include couple step :
Validate the date input according to our rule, for example because I use this date picker ( come from http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/index.html ), the date format default will be, dd/mm/yy. So we must ensure the date input format is correct. So I will use regular expression here :
$patt = '/^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[0-2])[\/](19|20)\d{2}$/';
After sure the date input is valid according our rule, we must also validate the input date according the calendar, for example, if the user input 31/2/2009 it will be valid in that regular expression check, but of course there no February 31 :p
$inputdate = explode("/",$_POST['inputdate']);
if ( ! checkdate($inputdate[1],$inputdate[0],$inputdate[2]) )
{
echo 'Invalid Date';
}
So here my complete example code snippet :
## date input pattern validation
$patt = '/^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[0-2])[\/](19|20)\d{2}$/';
if (! preg_match($patt,$_POST['inputdate']))
{
echo 'Invalid Date Format, mm/dd/yy';
die();
}
else
{
echo 'A Valid Date Format';
}
## calendar validation
$inputdate = explode("/",$_POST['inputdate']);
if ( ! checkdate($inputdate[1],$inputdate[0],$inputdate[2]) )
{
echo 'Invalid Date according Calendar';
die();
}
else
{
echo 'A Valid Date according Calendar';
}