Selecting DB Entries Between 2 Dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chopthis
    New Member
    • Feb 2008
    • 1

    Selecting DB Entries Between 2 Dates

    Hi,

    I have a database of users with their date of births in, D/M/YYYY.

    I want a search available so people can select between two ages.

    I would like to know how I can search between two ages, I'm guessing it involves working out the min and max dob, ie today is 9th feb 2008, so if i wanted to show people from 0 years old to 8 years old I need to select all those that fall within the date bracket of 9/2/2000-9/2/2008, but I am stumped.

    Thanks in advance
  • jenkinsloveschicken
    New Member
    • Dec 2006
    • 56

    #2
    Hi there chopthis,

    Welcome to TDSN!

    Not sure if I completely understand what you are after. Some more details would be helpful. However querying between dates isn't difficult.

    Just add the condition to the WHERE cause of your select.

    Code:
    SELECT * FROM sometable
    WHERE birthDate BETWEEN 'yourstartdate' AND 'yourenddate'
    As far as the scripting is concerned, if you are wanting to use a current date stamp to base the query on then you could just use the mktime function like so:

    Code:
    //create your current date
    $now = mktime(0, 0, 0, date("d"), date("m"), date("Y"));
    
    //Set query start and end dates
    $endDate = $now;
    $startDate = mktime(0, 0, 0, date("d"), date("m"), date("Y")-8);
    Then just pass those to your query like:

    Code:
    $myquery = mysql_query("SELECT * FROM mytable WHERE birthdate BETWEEN '$startDate' AND '$endDate'");
    You may need to tweak it a little to fit your specifics, but something like that should get your the results you need.

    Post back if you need additional assistance.

    Regards,
    Jenkins

    Comment

    Working...