title: PHP calendar with recurring events from MySQL database
body: <p><strong>UPDATE:</strong>
  Below is my original question. See my answer to see how I solved it.</p>
  
  <hr>
  
  <p>I am trying to populate my calendar with events from MySQL database table 'churchcal_events'. I might have one-time events for specific dates and recurring events that can be set for Every Monday, Every Other Thursday, or Every Month on Second Friday.</p>
  
  <p>One-time events are no problem. And weekly events work (every week, every other). But an <strong>every-month-event shows only on the first month, not on the months following</strong>.</p>
  
  <p><strong>churchcal_events table</strong> - excluding fields that are not important to this question</p>
  
  <pre><code>+----+----------------+------------+-----------+------------+------------+
  | id | name           | recurring  | frequency | recur_type | recur_day  |
  +----+----------------+------------+-----------+------------+------------+
  | 1  | Test Weekly    | 1          | 1         | W          | Sunday     |
  | 2  | Test Bi-Weekly | 1          | 2         | W          | Monday     |
  | 3  | Test Monthly   | 1          | 1         | M          | Friday     |
  +----+----------------+------------+-----------+------------+------------+
  </code></pre>
  
  <p><strong>php code</strong> - inside of a loop for each day of the month</p>
  
  <pre><code>//query all events
      $get_events = db_query("SELECT * FROM {churchcal_events}
      WHERE (MONTH(date) = :month AND YEAR(date) = :year AND DAY(date) = :day) OR
      (recurring = :recur AND recur_day LIKE :calendar_day)
      ORDER BY starttime",
      array(
        ':month' =&gt; $month,
        ':year' =&gt; $year,
        ':day' =&gt; $list_day,
        ':recur' =&gt; '1',
        ':calendar_day' =&gt; '%' . date('l', strtotime($month . '/' . $list_day . '/' . $year)) . '%',
      ));
  
      foreach($get_events as $event) {
  
        //see if events belong to this calendar
        $calendar_assign = db_query("SELECT * FROM {churchcal_assign} WHERE event_id = :event_id AND calendar_id = :cal_id",
        array(
          ':event_id' =&gt; $event-&gt;id,
          ':cal_id' =&gt; $cal_id,
        ));
  
        if($calendar_assign-&gt;rowCount() &gt; 0) {
  
          //if recurring, see if event should be on this day
          if($event-&gt;recurring == '1') {
  
            $recur_day = $event-&gt;recur_day;
            $recur_freq = $event-&gt;frequency;
            $recur_type = $event-&gt;recur_type;
  
            $recur_start = new DateTime(date('Y-m-d', strtotime($event-&gt;recur_start)));
            $recur_end = new DateTime(date('Y-m-d', strtotime($event-&gt;recur_end)));
  
            $recur_start-&gt;modify($recur_day);
  
            $recur_interval = new DateInterval("P{$recur_freq}{$recur_type}");
            $recur_period = new DatePeriod($recur_start, $recur_interval, $recur_end);
  
            foreach($recur_period as $recur_date) {
  
              if($recur_date-&gt;format('Ymd') == date('Ymd', strtotime($month . '/' . $list_day . '/' . $year))) {
  
                $calendar .= calendar_event($event-id, $event-&gt;name, $event-&gt;starttime);
  
              }
  
            }
  
          }
  </code></pre>
  
  <p><strong>How can I make ID '3' from the example churchcal_events table show up the first Friday of every month?</strong></p>