Jump to content

Search age range based on birthdate


AshleighCo
Go to solution Solved by AshleighCo,

Recommended Posts

I have an advanced search from that has 20 fields and it works great however I'm having a problem with customising the age range search based on the $birthdate field (YYYY/MM/DD). The user inputs $agefrom and $ageto and then I don't know how to convert the birthdate field to an age to be able to echo only the users in that age range...
 

<?php
$cQuery = 'true and ';
$aSearchcriteria = array();

//more code here...(other search input that works)

if(isset($_POST['agefrom']) and trim($_POST['agefrom'])  and strip_tags ($_POST['agefrom']) and mysql_real_escape_string ($_POST['agefrom'] ))
if(isset($_POST['ageto']) and trim($_POST['ageto'])  and strip_tags ($_POST['ageto']) and mysql_real_escape_string ($_POST['ageto'] )) {
             
  $cQuery .= "$birthdate BETWEEN '$agefrom' AND '$agefrom' and ";
  $aSearchcriteria[] = $agefrom;
  $aSearchcriteria[] = $ageto; 
}

//more code here...(other search input that works)

$data = mysql_query("SELECT * FROM table WHERE $cQuery order by name, surname") or die("SELECT Error: ".mysql_error());

// more code here

// table echo here...
?>
Link to comment
Share on other sites

You don't like to hang about for a reply, do you?

 

Calculate age by subtracting year of birth from year now, but if you haven't yet reached your birthday this year then subtract 1. Simples!

 

For example

SELECT thedate
, CASE WHEN DATE_FORMAT(CURDATE(),'%m%d') >= DATE_FORMAT(thedate,'%m%d')
    THEN YEAR(CURDATE()) - YEAR(thedate)
    ELSE YEAR(CURDATE()) - YEAR(thedate) - 1
    END as age
FROM dates
WHERE 
    CASE WHEN DATE_FORMAT(CURDATE(),'%m%d') >= DATE_FORMAT(thedate,'%m%d')
    THEN YEAR(CURDATE()) - YEAR(thedate)
    ELSE YEAR(CURDATE()) - YEAR(thedate) - 1
    END 
    BETWEEN $agefrom AND $ageto
Link to comment
Share on other sites

Here's some PHP code to determine the date range. It's a little odd since you use the lower age to determine the max date and the upper age to determine the min date. Also, for the min date, you have to go back age + 1 years and then increment +1 days. That's because a 40 year old person could be someone who's 40th birthday is today up to someone who is 40 years and 364 days old.

 

 

$min_age = 20;
$max_age = 40;
 
$max_date = strtotime("-{$min_age} years");
$max_date_str = date('Y-m-d', $min_date);
 
$max_age_offset = $max_age +1;
$min_date = strtotime("-{$max_age} years");
$min_date = strtotime("+1 day", $min_date);
$min_date_str = date('Y-m-d', $min_date);
 
echo "For the user to be from {$min_age} to {$max_age} years old they must
      have been born between {$min_date_str} and {$max_date_str}";
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.