I am trying to find if a time frame is availability from UNIX start to UNIX end. I have data like:
id start end
1 1309383000 1309390200
2 1305115200 1305129600
3 1305374400 1305403200
I am trying to run a query where I have a given start and end, and checking to see if this slow is taken. However, a start
can start when another entry ends. I have tried a lot of different ways, cannot get it to work right. Any help is appreciated.
I am using PHP to call the query. However, I would like to do it in pure MySQL if possible.
select if(max(end) >= $end, max(end), $end) -
if(min(start) <= $start, min(start), $start) -
if(sum(end - start) is null, 0, sum(end - start)) as FreeTime
where end > $start and start < $end
SELECT count(*)
FROM timeslots
WHERE ($your_start NOT BETWEEEN start AND end) AND
($your_end NOT BETWEEN start and end)
If that returns a count > 0, then the requested time slot is at least partially taken.
add 1 unit to startTime and subtract 1 unit from endTime, then use between clause for retrieving rows.