Her er noget, jeg har leget med (og her er det som en sqlfiddle
med nogle eksempeldata)... ikke 100 % sikker på det, men det burde have fat i de sidste 7 dages data. Bemærk, at jeg bruger MySQL DATETIME
versus heltal tidsstempler, men du burde være i stand til at konvertere det nemt (for at teste forespørgslen var det meget nemmere at bruge strengdatoer).
SELECT *
FROM
(SELECT
*,
CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', DAY(start)) AS monthly,
CONCAT(YEAR(NOW()), '-', MONTH(start), '-', DAY(start)) AS yearly
FROM events
) tmp
WHERE
(
(recurring = 'none')
OR (recurring = 'daily')
OR (recurring = 'weekly')
OR (
recurring = 'monthly'
AND (
(
monthly >= NOW()
AND monthly <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
OR (
DATE_ADD(monthly, INTERVAL 1 MONTH) >= NOW()
AND DATE_ADD(monthly, INTERVAL 1 MONTH) <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
)
)
OR (
recurring = 'yearly'
AND (
(
yearly >= NOW()
AND yearly <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
OR (
DATE_ADD(yearly, INTERVAL 1 YEAR) >= NOW()
AND DATE_ADD(yearly, INTERVAL 1 YEAR) <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
)
)
)
AND start <= NOW()
AND (
end IS NULL
OR end >= DATE_ADD(NOW(), INTERVAL 7 DAY)
)