EchoFool Posted November 3, 2011 Share Posted November 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/ Share on other sites More sharing options...
Pikachu2000 Posted November 3, 2011 Share Posted November 3, 2011 You're adding 18 years and 60 years to the current date then seeing if a date in 1978 is between them. I can assure you, it is not. Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284500 Share on other sites More sharing options...
EchoFool Posted November 3, 2011 Author Share Posted November 3, 2011 Oops my bad, it was meant to be 18 and 99 not 60 =/ Do you mean i have to convert "age" from date to years old first.. how can i do that in MySQL =/ Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284501 Share on other sites More sharing options...
Pikachu2000 Posted November 3, 2011 Share Posted November 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284502 Share on other sites More sharing options...
EchoFool Posted November 3, 2011 Author Share Posted November 3, 2011 I shall take a look at that thanks! p.s is there a way to not take into account leap years? I originally got the DATE_ADD() from here http://www.phpfreaks.com/forums/index.php?topic=345577 Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284504 Share on other sites More sharing options...
Pikachu2000 Posted November 3, 2011 Share Posted November 3, 2011 I suppose you could also use DATE_SUB() instead of DATE_ADD() with those same values, and see if the date of birth is between those dates. That would probably work too, but I haven't tested it myself. Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284505 Share on other sites More sharing options...
EchoFool Posted November 3, 2011 Author Share Posted November 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284506 Share on other sites More sharing options...
Pikachu2000 Posted November 3, 2011 Share Posted November 3, 2011 That's to determine if the birthday has passed or not in the current year. If it hasn't, the age is one less than the value of current year - birth year. Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284621 Share on other sites More sharing options...
EchoFool Posted November 3, 2011 Author Share Posted November 3, 2011 Oh i see, thanks for the advice Quote Link to comment https://forums.phpfreaks.com/topic/250347-date-comparison-issue/#findComment-1284726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.