I have piece of code that works without making it a function. But, when I make it a function, it always returns false. The purpose of this function is to check if the date is valid, as the name suggests. Can anybody tell me what is wrong with the code ?
function is_valid_date($a) {
//date format Y-m-d H:i:s
if(preg_match('/^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01]) ([01]\d|2[0123]):([0-5]\d):([0-5]\d)$/', $a)){
list( $_date , $_time ) = explode(' ',$a);
list ($year,$month,$day) = explode("-",$_date);
list($hour,$minute,$second) = explode(":",$_time);
if ($day == "31" && ($month == "4" || $month == "6" || $month == "9" || $month == "11" || $month == "04" || $month == "06" || $month == "09" )) {
return false;
} elseif ($month == "2" || $month == "02") {
if($year % 4==0){
if($day == "30" || $day == "31"){
return false;
} else {
return true;
}
}else{
if($day == "29" || $day == "30" || $day == "31"){
return false;
} else {
return true;
}
}
}
}else{
return false;
}
}
when I try
if (is_valid_date("2012-12-02 15:30:00")) { echo "valid date";}
nothing is printing.
trueonlyelse if month=2