By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Selecting DB Entries Between 2 Dates

P: 1
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
Feb 9 '08 #1
Share this Question
Share on Google+
1 Reply


jenkinsloveschicken
P: 56
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM sometable
  3. WHERE birthDate BETWEEN 'yourstartdate' AND 'yourenddate'
  4.  
  5.  
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:

Expand|Select|Wrap|Line Numbers
  1. //create your current date
  2. $now = mktime(0, 0, 0, date("d"), date("m"), date("Y"));
  3.  
  4. //Set query start and end dates
  5. $endDate = $now;
  6. $startDate = mktime(0, 0, 0, date("d"), date("m"), date("Y")-8);
  7.  
  8.  
Then just pass those to your query like:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $myquery = mysql_query("SELECT * FROM mytable WHERE birthdate BETWEEN '$startDate' AND '$endDate'");
  3.  
  4.  
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
Feb 9 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.