simple php if statement help

Soldato
Joined
25 Jan 2003
Posts
11,497
Location
Newark, Notts
i'm new to php, and I cant figure out how to do this if statement.

I've got a registration form and I'm trying to add validation to each of the inputs, but i'm stumped on the date of birth one. If users aren't 18 I need it to tell them that they cant register. What I came up with is below, and it doesn't seem to wanna work (probably cos its completely wrong :) ):

if ($year < 1988 and $month > 10) then;
echo "You must be 18 years of age to register";

How do i need to change it? apart from the actual maths regarding how it works out that they are 18 cos i know thats wrong already
 
Last edited:

daz

daz

Soldato
Joined
18 Oct 2002
Posts
24,062
Location
Bucks
Try:-

if ($year < 1988 && $month < 10)
{ echo "You must be 18 years of age to register"; }
else
{ whatever; }
 
Soldato
OP
Joined
25 Jan 2003
Posts
11,497
Location
Newark, Notts
nah still not working really as I want it to. At the moment if someone tries to register who were born in 2003 they dont get the message if they enter a month lower than 10. How can i change this to work properaly? :confused:

And how can i get this to work with the system date or something, because in a years time an 18 year old wouldn't be able to register based on the >1988 stuff in there at the moment
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
Code:
function valid_birthdate( $year, $month, $day ) {
    // Cut out anyone who was born after 1988
    if ( $year > 1988 )
        return false;

    // Cut out anyone who was born before October 1988
    if ( $year == 1988 & $month < 10 )
        return false;

    // Everyone else is fine.
    return true;
}

Make it dynamic, though.

Edit: To make it dynamic:

Code:
function valid_birthdate( $year, $month, $day ) {
    $cutoff_year = intval(date('Y')) - 18;

    if ( $year > $cutoff_year )
        return false;

    if ( $year == $cutoff_year & $month < intval(date('m')) )
        return false;

    return true;
}

You should do days too though!
 
Last edited:
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
robmiller said:
[snip]

Make it dynamic, though.

Edit: To make it dynamic:

[snip]

You should do days too though!
Don't use bitwise logic operators in boolean expressions
emot-eng101.gif


Also, shouldn't you be checking that $month > intval(date('m'))? If the month of their birth is later than the current month, then they're not 18 yet.
 
Last edited:
Soldato
Joined
12 Apr 2004
Posts
11,788
Location
Somewhere
Ripper^ said:
what do i need to put in there to alert the user on the page that they need to be 18? echo "blablabla"; im guessing, but where?
Rob's basically just written a function to check the age for you, so that the code is abstracted from your actual page logic. Just stick it in an included file, or at the top of the page's .php file, then use it in the script as follows:

Code:
// ...

// Check that user is old enough
$isOldEnough = validate_birthdate($year, $month, $day);
if (!$isOldEnough)
{
    // User is not old enough
    echo 'Come back later.';
}
else
{
    // User is old enough
}

// ...
 
Soldato
OP
Joined
25 Jan 2003
Posts
11,497
Location
Newark, Notts
Cheers guys i think it works now :)

isset said:
I'm sure your tutor at Staffs uni would just love to see this thread ;)

Why's that? i'm being pointed in the right direction and trying to learn how to do something that we havn't been taught. I'm not going to learn php without someone/something explaining how things are done.
 
Man of Honour
Joined
31 Jan 2004
Posts
16,334
Location
Plymouth
Ripper^ said:
Cheers guys i think it works now :)



Why's that? i'm being pointed in the right direction and trying to learn how to do something that we havn't been taught. I'm not going to learn php without someone/something explaining how things are done.
You must have been taught basic language constructs, though :)

In our introductory lecture my tutor said "sure, ask me which books to read - but if you find the answer in a book, I'll change the question" :)
 
Associate
Joined
16 May 2003
Posts
444
Location
Surrey
Ripper^ said:
Why's that?

Because you have absolutely definitely been taught basic language constructs just as Beansprout said. And because it's no different to what you did in the first year in ASP - just a slightly different syntax, so you've been taught the same principles twice now, or even three times if you did Web Applications .NET. Aaand because you've probably copied directly, and not taken the time to fully understood robmiller's code. ;)
 
Last edited:
Associate
Joined
21 May 2003
Posts
1,365
Here's another example but using mktime()....

Code:
function validDob($dobDay,$dobMonth,$dobYear) {
	$dateOfBirth = mktime(0,0,0,$dobMonth,$dobDay,$dobYear);	
	$todayMinus18Years = mktime(0,0,0,date('m'),date('d'),date('Y')-18);
		
	if($dateOfBirth > $todayMinus18Years){
		return false;
	} else {
		return true;
	}
}

*edit* - This one does days too.
 
Back
Top Bottom