Jump to content

How to select users within a range of age's


Accurax

Recommended Posts

This might be a little complicated,

 

In my database i have the date of birth of my members stored in 3 fields, 'year' 'month' ' day'.

 

I'm using the following code throughout my site, to calculate the age of a given user at any point and echo it onto the page.

 

<?php
day = $row['day'];
$month = $row['month'];
$year = $row['year'];
$birthday = $day."-".$month."-".$year;
$today = date('d-m-Y');

$a_birthday = explode('-', $birthday);
$a_today = explode('-', $today);

$day_birthday = $a_birthday[0];
$month_birthday = $a_birthday[1];
$year_birthday = $a_birthday[2];
$day_today = $a_today[0];
$month_today = $a_today[1];
$year_today = $a_today[2];

$age = $year_today - $year_birthday;

if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday))
{
$age--;
}
echo "<strong> - Age: </strong>";
echo '  ';
echo $age;
?>

 

The problem

 

I now have a search form with which i want to allow my users to search for other members within a given age raneg... 25-78 for example. The form has two fields, one for the uppper age, and one for the lower age limits, and i only want to return entries between this criteria.

 

Can anyone give me some idea on how to achieve this? ...... a push in the right direction would be greatly appreciated

I agree 100% with monk.e.boy on this one. You need to use the MySQL datatypes for what they are intended. If you have too much data to do this, you may consider a workaround similar to this:

SELECT * FROM userTable
WHERE DATE(CONCAT(bday_year, '-', bday_month, '-', bday_day))
BETWEEN CURDATE() - INTERVAL 78 YEAR AND CURDATE() - INTERVAL 25 YEAR;

 

Hope this helps.

lacking in the sense that A) i dont need a workaround and B) i didnt understand a word of what he said

 

The way i see it, all i need to do is convert the dob's in my database into ages, and compare them to the values entered in the form.

 

How i do this is what im trying to figure out.

 

I'm not asking for a solution here, im more asking for help in getting my head around it and actually understanding how it works..... whats the point in doingf it if i just copy code from other people?

The way i see it, all i need to do is convert the dob's in my database into ages, and compare them to the values entered in the form.

 

That's exactly what I was trying to help you do. Since you are storing the values of the month, day and year separately, you'll need to tell MySQL that it is a date you are after by combining the columns manually. To do this, you need to concatenate the values with hyphens to get it in the appropriate format for SQL to recognize. Then, using the DATE() function around it makes it that much more explicitly stated. Finally, using the INTERVAL subtraction, you can easily compare the date ranges you're after.

lacking in the sense that A) i dont need a workaround and B) i didnt understand a word of what he said

 

The way i see it, all i need to do is convert the dob's in my database into ages, and compare them to the values entered in the form.

 

How i do this is what im trying to figure out.

 

I'm not asking for a solution here, im more asking for help in getting my head around it and actually understanding how it works..... whats the point in doingf it if i just copy code from other people?

 

Well, there is this cool tool called google, if you type things like 'mysql CURDATE' into it, it tells you the answer  ;D ;D ;D

 

But see how obsidian is casting your data format into a DATE then doing the calculation. You should just store it as a date.

 

monk.e.boy

Hey, guys, I just removed the last few posts that were made. Not only were they completely unrelated to the topic, but they were not informative at all, and they could be (and were) taken as insulting. Please try to refrain from any personal attacks on the boards. I know we all have bad days sometimes, but we're all here to help out, so let's try to keep the spirit of help about us and not talk down to someone because they don't know something we do. Chances are, if we're willing to listen, every person on this board could tell us something we don't know, too ;)

 

Thanks for your help with this!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.