If a slot is recurring it happens at the same time each week.
How would I fetch records scheduled later in the day, week, or month, considering the current date and time? I am using MySQL.
My current query is
SELECT *
FROM `timetable`
WHERE (`week` >= {$week} OR `recurring` = 1)
AND `day` >= {$day}
AND `hour` >= {$hour}
LIMIT 3
2
Answers
Presumably you would want to include all weekly-recurring events, since there will always be one ahead of your current week/day/hour. I also assume you would want all events that come after the current week, irrespective of the day, and within the current week, you would want all the events that come after the current day, irrespective of the hour.
If this is all correct, then your condition becomes:
Later in the day
Explanation:
the_time
, use yours instead of this oneLater in the week
Here the logic is the same, except for the end of the interval, where we add as many days as the number of days still left from the week. We add 7 for Monday, 6 for Tuesday, etc.
Now,
WEEKDAY
returns 0 for Monday, 1 for Tuesday, etc.So, 7 – WEEKDAY will result in the exact difference we want.
Later in the Month
Similar query, but with the start of the next month.
General remarks
If you may have events at midnight, then you might want to subtract a second from the rightmost ends of the interval or use
<=
and<
instead ofbetween
. I avoided those for the sake of understandability.Also, this way you find recurring events, but you find them once. So, if you have a Friday night Karaoke each Friday night and it is scheduled to happen twice from the remainder of this month, this query will find it once.
If you want to show the recurring events for month view as many times they are scheduled for the month, you can use unioned queries or stored procedures.