I have some events in a MySQL database and I need to extract them to feed a calendar. Normal events are not a problem but I have some special events that could repeat on a yearly basis. My problem is how to extract these events. I tried this to get events in the interval [$ini_month, $ini_day] – [$end_month, $end_day]:
SELECT * FROM `events`
WHERE `repeat`='1' AND
MONTH(`date`) BETWEEN '$ini_month' AND '$end_month' AND
DAY(`date`) BETWEEN '$ini_day' AND '$end_day'
For instance, if I have to get events from July 1st to July 7th, $ini_month=$end_month=7, $ini_day=1 and $end_day=7. In this case this query works, but if I have to get events from June 25th to July 7th it fails $end_day < $ini_day. So, please, could you help me to generalize this query to make it work against any input?
You have to treat the “boder months” specially.
should work. But this is extremely ugly.
Instead, you could do something like
if you set
$ini_month_dayand$end_month_dayto appropriate values.Be aware that this isn’t optimal as well, as you cannot use indexes this way.