I have users in a MySQL database where their birthdays are stored in the format DATE(yyyy-mm-dd). I want to select users in a database with PHP who are between a certain age range.
I have a minimum age (18) and a maximum age (21). I know that I can do something BETWEEN, but the problem is that I only know years, not dates.
Does anyone have a suggestion on how I can do this?
This is what I am doing now:
function leeftijden($age) {
$morgen['day'] = date('d');
$morgen['month'] = date('m');
$morgen['year'] = date('Y') - $age;
$datum = $morgen['year'] .'-' .$morgen['month'].'-' .$morgen['day'];
return $datum;
}
Then I do this:
$maxDatum = leeftijden(18);
$minDatum = leeftijden(32);
$sqlRijder = "SELECT * FROM rijder WHERE geboortedatum between '".$minDatum."' AND '".$maxDatum."'";
But it does not work 100%.
How can I select only users who are between 18 and 21 years old?
source
share