Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Date Range

Status
Not open for further replies.

SBuzzT

Programmer
Aug 24, 2005
86
CA
I need to select all the entries for a 7 day period based on 1 date being supplied. In other words, if 2006-01-01 is selected, all entries from (and including) 2006-01-01 to(and including) 2006-01-07 would be returned. I am using a MySQL database.

Any ideas?
 
I played around with that, but was unable to get it to work correctly.
 
No matter how I write the query, I can seem to isolate the 1 week. I either get all the result less than and equal or greater than and equal to the supplied date. In other words, I only want results from 2006-01-01 through 2006-01-07. Somehow, I manage to also get results from more than 1 week before or after as well.
 
SELECT * from TABLE where TO_DAYS(DATE_COL) - TO_DAYS('$week')<=7
 
You have your column and date reversed.

On my MySQL installation, this query:

SELECT * from foo where TO_DAYS('2006-04-11') - TO_DAYS(the_date)<=7

works just fine selecting the last 7 days.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Yes, but what I am trying to do is isolate THAT week. The way the query is now, it will also give results from before the selected date. The selected date should be the start of the results and find nothing exceeding 7 days later.
 
Again, I'd use something like adddate():

select * from foo where the_date >= '2006-04-02' AND the_date <= adddate('2006-04-02', interval 7 day);




Want the best answers? Ask the best questions! TANSTAAFL!
 
Sigh...

I gave up on the MySQL method and went with this ($week is the selected date). Works...
Code:
$week2  = date('Y-m-d', strtotime ("+1 week $week"));
$sql    = mysql_query("SELECT * from TABLE where DATE_COL between '$week' and '$week2'");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top