PHP Search By Age Using Date

Discussion in 'PHP' started by audax, Oct 14, 2007.

  1. #1
    I am currently trying to adjust a search function on a website I'm working on to search through a group of people by searching by age for a group of different ages: 0-12, 13-17, 18-30, 31-49, and 50+.
    The problem I'm running into is when a person is, say, 31 but comes up under the 18-30 search. This is what the previous web designer set up for the search function:

    
    $age = $_GET['age'];
    
    $thisyear = date('Y');
    
    if($age == 'kid') 	{$youngest = date('Y-m-d');  $oldest = $thisyear - 13;}
    if($age == 'teen') 	{$youngest = $thisyear - 13;  $oldest = $thisyear - 18;}
    if($age == 'youngadult'){$youngest = $thisyear - 18;  $oldest = $thisyear - 32;}
    if($age == 'adult') 	{$youngest = $thisyear - 32;  $oldest = $thisyear - 50;}
    if($age == 'elderly') 	{$youngest = $thisyear - 50;  $oldest = $thisyear - 120;}
    
    $oldest = $oldest.date('-m-d');
    $youngest = $youngest.date('-m-d');
    
    PHP:
    The dates of birth are entered in the database as YYYY-MM-DD, listed as dob. He then made the search by age function to query the database similar to something as follows:

    $query = "SELECT * FROM people";
    $query .= " WHERE (dob <= '$youngest' AND dob >= '$oldest')";
    PHP:
    What is wrong that is making people show up under the wrong age groups for the search?
     
    audax, Oct 14, 2007 IP
  2. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just an update: I think I fixed the problem by switching a few obvious numbers around, but I was wondering if there was a better way to compare dates, like with strtotime or something. Anyone have any idea on how to find out ages and such with strtotime? Would it be considered a short-cut if I used that instead of comparing the dates in YYYY-MM-DD format?
     
    audax, Oct 16, 2007 IP