tags:

views:

31

answers:

1

Currently I have two tables and the following sql statement which correctly retrieves the items in the events table ordered by their dates in the event_dates table:

SELECT * FROM events, event_dates 
WHERE events.id=event_dates.event_id 
     AND events.preview=0 AND event_dates.start_date>=now() 
ORDER BY event_dates.start_date ASC,event_dates.start_time ASC LIMIT 3

Now I want to add an extra AND to make sure only the events on the next weekend are set. The date column is in a standard mysql date format (YYYY-MM-DD). Got stuck on this bit. Cheers.

+3  A: 

Use PHP strtotime() to get the start and end timestamp of the weekend:

$we_start=strtotime('next saturday');
$we_end=strtotime('next monday')-1;

Then do a sql query to search for timestamps BETWEEN them.

select * from mytable where UNIX_TIMESTAMP(mydatefield) BETWEEN $we_start AND $we_end

Hope that helps.

Spudley
+1 Better than what I had in mind :) Although, the order of arguments for `BETWEEN` matters (http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between) so you should probably do `$real_we_start = min($we_start, $we_end)` and `$real_we_end = max($we_start, $we_end)` before querying the DB.
jensgram
Of course, thats perfect
kalpaitch
Or, you could: `$we_start=strtotime('next saturday');` and `$we_end=strtotime('next monday', $we_start)-1;`. I'll be quiet now :)
jensgram