In this we will see date validation function written in PHP.
For using this function date has to be separated with “-” or “/” or u can change to your favorite one. If anyone have any queries or suggestions they are welcome.
function date_validate($datefield) {
// First check to see if the input ($datefield) is in one of the accepted formats
// Check for delimiters ("-" or "/") and put three fields into an array
if (strpos($datefield, "-")) {
$datesplit = explode("-", $datefield);
} elseif (strpos($datefield, "/")) {
$datesplit = explode("/", $datefield);
} else {
$date_err="Error: Invalid date field. No proper delimiters (- or /) found";
return false;
}
// Check for three input fields (month, day, year)
if (count($datesplit)>3) {
$date_err="Error: Invalid date field. Too many fields (".count($datesplit).") found";
return false;
}
// Put date array into single format
if (strlen($datesplit[2])==4) { // The year is listed last - switch fields around
$newdatesplit[0]=$datesplit[2]; // Move Year to first field
$newdatesplit[1]=$datesplit[0]; // Move Month to second field
$newdatesplit[2]=$datesplit[1]; // Move Day to third field
$datesplit=$newdatesplit;
} elseif (strlen($datesplit[0])==4) { // The year is first listed - do nothing
// nothing to be done
} else { // Date entered is not valid; could not find year field
$date_err=”Error: Date not valid. No Year field found (Year must be 4 digits)”;
return false;
}
// Main validation code
if ($datesplit[1]>12) { // No valid month field
$date_err=”Error: Invalid Month field (”.$datesplit[1].”) “;
return false;
} else {
switch ($datesplit[1]) { // Check number of days in a month
case 4:
case 6:
case 9:
case 11:
if ($datesplit[2]>30) {
$date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
return false;
}
break;
case 2: // February Check
if (($datesplit[0]/4)==(floor($datesplit[0]/4))) {
if (($datesplit[0]/100)==(floor($datesplit[0]/100))) {
if (($datesplit[0]==1600) or ($datesplit[0]==2000) or ($datesplit[0]==2400)) {
if ($datesplit[2]>29) {
$date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
return false;
}
} else {
if ($datesplit[2]>28) {
$date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
return false;
}
}
} else {
if ($datesplit[2]>29) {
$date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
return false;
}
}
} else {
if ($datesplit[2]>28) {
$date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
return false;
}
}
break;
default:
if ($datesplit[2]>31) {
$date_err=”Error: Invalid # of days (”.$datesplit[2].”) for month “.$datesplit[1].” and year “.$datesplit[0];
return false;
}
}
}
// Add leading zero if month or day field is only one character
if (strlen($datesplit[1])==1) {
$datesplit[1]=”0″.$datesplit[1];
}
if (strlen($datesplit[2])==1) {
$datesplit[2]=”0″.$datesplit[2];
}
// Create date field in MySQL format
$newdate=$datesplit[0].”-”.$datesplit[1].”-”.$datesplit[2];
return true;
}