title: Attempting to group events by date in PHP
body: <p>I'm grabbing a list of events from a database and am trying to associate like dates with one header. As of the moment I'm using <code>&lt;dl&gt;</code> <code>&lt;dt&gt;</code> <code>&lt;dd&gt;</code>, etc as per request. This may change but I don't think it impacts the overall impact of my question.</p>
  
  <p>Basically I am trying to grab the 5 newest events via a query:</p>
  
  <pre><code>global $wpdb;
  
  $sql = "SELECT e.*, c.*, ese.start_time, ese.end_time
          FROM wp_events_detail e
          JOIN wp_events_category_rel r ON r.event_id = e.id
          JOIN wp_events_category_detail c ON c.id = r.cat_id
          LEFT JOIN wp_events_start_end ese ON ese.event_id= e.id 
          WHERE e.is_active =  'Y'
          AND c.category_identifier = 'main'
          ORDER BY e.start_date ASC
          LIMIT 0, 5";
  $results = $wpdb-&gt;get_results($sql);
  </code></pre>
  
  <p>I then loop them through a <code>foreach()</code>: </p>
  
  <pre><code>if(count($results) &gt; 0) {
  
      echo '&lt;div class="calendar-list"&gt;';
  
      foreach($results as $event) {
  
          $event_id = $event-&gt;id;
          $event_name = $event-&gt;event_name;
          $start_date = $event-&gt;start_date;
          $start_time = $event-&gt;start_time;
  
          $start_time = strtotime($start_time);
          $start_time = date('g:ia', $start_time);
  
          $the_time =  strtotime($start_date);
  
          $the_day = date('d', $the_time);
          $the_month = date('M', $the_time);
  
          echo '&lt;dl class="calendar-day first-child"&gt;';
          echo '&lt;dt&gt;';
          echo '&lt;p class="the-month"&gt;' . $the_month . '&lt;/p&gt;';
          echo '&lt;p class="the-day"&gt;' . $the_day . '&lt;/p&gt;';
          echo '&lt;/dt&gt;';
          echo '&lt;dd&gt;';
          echo '&lt;h4&gt;&lt;a href="' . $event_id . '"&gt;' . $event_name . '&lt;/a&gt;&lt;/h4&gt;';
          echo '&lt;h5&gt;&lt;span class="time"&gt;' . $start_time . '&lt;/span&gt;&lt;/h5&gt;';
          echo '&lt;/dd&gt;';
          echo '&lt;/dl&gt;';
  
      }
  
      echo '&lt;/div&gt;';
  }
  </code></pre>
  
  <p>My main problem is I need to figure out a way to associate dates with one another. For example, June 27th. If there are two dates I would like it to look like:</p>
  
  <pre><code>&lt;div class="calendar-list"&gt;
      &lt;dl class="calendar"&gt;
          &lt;dt&gt;
              &lt;p class="the-month"&gt;JUN&lt;/p&gt;
              &lt;p class="the-day"&gt;27&lt;/p&gt;
          &lt;/dt&gt;
          &lt;dd&gt;
              &lt;h4&gt;&lt;a href="#"&gt;Title&lt;/a&gt;&lt;/h4&gt;
                          &lt;h5&gt;&lt;span class="time"&gt;Time&lt;/span&gt;&lt;/h5&gt;
          &lt;/dd&gt;
          &lt;dd&gt;
              &lt;h4&gt;&lt;a href="#"&gt;Title&lt;/a&gt;&lt;/h4&gt;
                          &lt;h5&gt;&lt;span class="time"&gt;Time&lt;/span&gt;&lt;/h5&gt;
          &lt;/dd&gt;
      &lt;/dl&gt;
      &lt;dl class="calendar"&gt;
          &lt;dt&gt;
              &lt;p class="the-month"&gt;JUN&lt;/p&gt;
              &lt;p class="the-day"&gt;28&lt;/p&gt;
          &lt;/dt&gt;
          &lt;dd&gt;
              &lt;h4&gt;&lt;a href="#"&gt;Title&lt;/a&gt;&lt;/h4&gt;
                          &lt;h5&gt;&lt;span class="time"&gt;Time&lt;/span&gt;&lt;/h5&gt;
          &lt;/dd&gt;
          &lt;/dl&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I am quite unsure how to achieve this. I have been playing around with setting and unsetting some variables but have been unable to achieve the desirable outcome. </p>
  
  <p>If someone could point me in the right direction of what to do, it would be most appreciated.</p>
  
  <p>Thank you!</p>
  
  <p>Tre</p>