Skip to content

Instantly share code, notes, and snippets.

@crossan007
Created November 30, 2018 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crossan007/d1cbc8e7e8cc7334896dd8067e5de95a to your computer and use it in GitHub Desktop.
Save crossan007/d1cbc8e7e8cc7334896dd8067e5de95a to your computer and use it in GitHub Desktop.
/* Add Recurring event, with a secondary calendar on the Third Tuesday of the Month */
DELIMITER $$
DROP PROCEDURE IF EXISTS create_events$$
CREATE PROCEDURE create_events()
BEGIN
DECLARE event_date DATETIME;
DECLARE event_end DATETIME;
DECLARE ocurrences INT;
DECLARE last_inserted INT;
DECLARE week_of_month DOUBLE;
SET event_date = '2018-11-27 18:30:00';
SET ocurrences = 52;
WHILE ocurrences > 0 DO
SET event_date = DATE_ADD(event_date, INTERVAL 7 DAY);
SET event_end = DATE_ADD(event_date, INTERVAL 2 HOUR);
INSERT INTO events_event (event_title, event_type,event_start,event_end) VALUES ('Life Group', 1, event_date, event_end);
SELECT LAST_INSERT_ID() INTO last_inserted;
/*
Get which numbered "tuesday" this si
https://stackoverflow.com/questions/785206/function-for-week-of-the-month-in-mysql
*/
SELECT (dayofmonth(event_date) + 6 + (7 - "1") - (weekday(event_date) - "2" + 7) MOD 7) DIV 7 into week_of_month;
INSERT INTO calendar_events (calendar_id,event_id) VALUES (7, last_inserted);
IF week_of_month = 3 THEN
INSERT INTO calendar_events (calendar_id,event_id) VALUES (2, last_inserted);
END IF;
SET ocurrences = ocurrences -1;
END WHILE;
END$$
DELIMITER;
CALL create_events();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment