Jump to content

Date Comparison issue


EchoFool

Recommended Posts

Hey

 

 

I have a WHERE clause which has this:

 

 

<?php
$hi = 99;
$low = 18;


//WHERE clause
age BETWEEN (DATE_ADD(CURDATE(), INTERVAL $low YEAR) AND DATE_ADD(CURDATE(), INTERVAL $hi YEAR)) 
?>

 

 

The field age is in the structure of YYYY-MM-DD

 

Now i have in the database 1 user with this:

 

Age = 1978-05-29

 

 

But my query returns 0 results. The WHERE clause is suppose to find all users whose age is between the two values.

 

 

Can any one see the mistake i am making here?

Link to comment
Share on other sites

What do you think the DATE_ADD() function in MySQL does? You are adding 18 years to the current date, and you are adding 60 years to the current date, then checking whether a date 33 years in the past is between them. What you need is to calculate the user's age, then see if that number is between 18 and 60.

 

See this post on the MySQL forums for a good way to calculate age.

Link to comment
Share on other sites

The one you provided me is a little confusing.

 

((date_format(now(),'%Y') - date_format(ma.DOB,'%Y')) - (date_format(now(),'00-%m-%d') < date_format(ma.DOB,'00-%m-%d'))) AS age 

 

How come it does 3 subtractions then compares if its less than something when calculating the age?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.