php条件表单按日期验证

I have the function below that takes an entered date and serves different validation messages base don the dates value .The thing is is not always working as it's supposed. I'm new at PHP and maybe i'm doing something wrong. Anyone has an ideea how to correct it?

function validate_date($result, $value, $form, $field){

$date1=date("Y", strtotime($value));
$date2=date("Y");
$diff= $date2-$date1;
$diff2=18-$diff;


if ($value ==0) { 
    $result["is_valid"] = false;
    $result["message"] = "Compeltati data!";
}
else {
    if ($diff < 0) { 
        $result["is_valid"] = false;
        $result["message"] = "Data invalida!";
    }
    elseif ($diff == 0) {
        $result["is_valid"] = false;
        $result["message"] ="Data invalida!";
    }
    elseif ($diff > 0){
        if ($diff2 > 0 and $diff2 < 5 ){
            $result["is_valid"] = true;
            echo '<script type="text/javascript">alert("Va trebuie confirmarea parintilor. Contactati-ne pe email!");</script>';
        }
        elseif ($diff2 > 5) {
            $result["is_valid"] = false;
            $result["message"] = "Nu aveti varsta minima pentru participare!";
        }
    }
}

return $result;
}

As I understand $diff is AGE and $diff2 is how much he/she is younger then 18year age. In that case if age of person is more then 18 then $diff2 will be negative and you have'nt added any clause for $diff2<0. so add it like:

if ($diff2 > 0 and $diff2 < 5 ){ $result["is_valid"] = true; echo '<script type="text/javascript">alert("Va trebuie confirmarea parintilor. Contactati-ne pe email!");</script>'; } elseif ($diff2 > 5) { $result["is_valid"] = false; $result["message"] = "Nu aveti varsta minima pentru participare!"; } else { //for $diff<=0 $result["is_valid"] = false; //as you need change it on age >=18 $result["message"] = "as you need!"; }