Newbie help please && not evaluating

Hello,

I have been banging my head against a brick wall trying to see what I’m doing wrong.

In the following code (test if person submitting form is aged over 18 based on birthdate) “1” is correctly returned if the year of birth is 1988 or before.

However both subsequent tests return “0” even when the input should evaluate to being over 18. (The input variable names from the form are correct.)

I’m truly stuck!

$birthyear = $_REQUEST['birthyear'];
$birthmonth = $_REQUEST['birthmonth'];
$birthday = $_REQUEST['birthday'];
$today = date("d");
$thismonth = date("M");
$thisyear = date("Y");


if($thisyear - $birthyear > 18){
$ageflag = "1";
}
elseif (($thisyear - $birthyear = 18) && ($thismonth - $birthmonth > 0)){
$ageflag = "2";
}
elseif (($thisyear - $birthyear = 18) && ($thismonth - $birthmonth = 0) && ($today - $birthday >= 0)){
$ageflag = "3";
}
else {
$ageflag = "0";
}

ADMIN EDIT: Changed COLOR tag to CODE for easier distinction of code from other text

elseif (($thisyear - $birthyear = 18) && ($thismonth - $birthmonth > 0)){

What does this code do? Especially the first part. You’re using an assignment mark (=) to make a comparison. Try using this:

elseif ((($thisyear - $birthyear) == 18) && ($thismonth - $birthmonth > 0)){

BUT …

Why do you use multiple elseif constructoins when a simple comparison between timestamps works perfectly down to the minute?

$birthyear = $_REQUEST['birthyear'];
$birthmonth = $_REQUEST['birthmonth'];
$birthday = $_REQUEST['birthday'];

$mydate = mktime(0, 0, 0, $birthmonth, $birthday, $birthyear + 18);

if ($mydate <= time()) {
  // User is 18 years or older
}

Thank you - that worked!

I haven’t programmed anything for 20 years but the BASIC syntax had stuck and thrown me, hence the single “=”. The code is now fully functional and live although probably not as streamlined as it might be.

Sponsor our Newsletter | Privacy Policy | Terms of Service