views:

95

answers:

1

I am creating a calendar application. In the Events table, there are two columns, a start and end timestamp.

EVENTS - { start_timestamp, end_timestamp }

I have two variables in my PHP application. One is the timestamp of the beginning of a month, another is the timestamp of the end of a month.

I want to select from the events table if that event is inside the month. Somehow I need to test for intersection between the ranges of the start and end timestamps of each event, and the beginning and end of the month. Is this possible? Are there any better methods to accomplish this?

+4  A: 

If you want to check for events that occur within the month, but may start before the beginning of the month and end after the end of the month, then use this

Select * from Events 
where start_timestamp <= MonthEndTimestamp 
and end_timestamp >= MonthStartTimestamp

However, I think there's more to your question I'm not understand, so if this is not sufficient, please clarify.

Kibbee
Thanks, this looks like it'll work.
JasonV
If he is trying to include them even if they start/end outside the month, shouldn't the conditions be ORed not ANDed?
eidylon
@eidlyon: No, it's correct. Look carefully at the expression, and draw a truth-table if necessary. The only false result is when *both* timestamps are before the month-start, or *both* are after the month-end.
Bill Karwin