Answers.Body: <p>Check out the <a href="https://craftcms.com/docs/5.x/reference/twig/filters.html#group" rel="nofollow noreferrer">documentation for the group filter</a> and you'll realise that it creates a nested array for each thing you want to group by, and the array key of each nested array will be your eventStartTime. Once you have your grouped array you can then cherrypick today's events, tomorrow's events, and the rest using the <a href="https://craftcms.com/docs/5.x/reference/twig/filters.html#filter" rel="nofollow noreferrer">filter</a> filter, combined with Twig's <a href="https://twig.symfony.com/doc/3.x/filters/date.html" rel="nofollow noreferrer">now</a> variable, and the <a href="https://twig.symfony.com/doc/3.x/filters/date_modify.html" rel="nofollow noreferrer">date_modify filter</a>. I haven't tested this but it should work:</p>
  <pre class="lang-twig prettyprint-override"><code>{# Fetch all events from the matrix #}
  {% set eventsMatrix = entry.liveStreamEvents.all() %}
  
  {# Group the array by date in the format 2024-12-31 #}
  {% set eventsGroupedByStartDate = eventsMatrix|group(&quot;eventStartTime|date('Y-m-d')&quot;) %}
  
  {# Filter out today's and tommorrow's events by comparing to `now` #}
  {% set todaysEvents = eventsGroupedByStartDate|filter(date =&gt; date == now|date('Y-m-d')) %}
  {% set tomorrowsEvents = eventsGroupedByStartDate|filter(date =&gt; date == now|date_modify('+1 day')|date('Y-m-d')) %}
  {% set beyondTomorrowsEvents = eventsGroupedByStartDate|filter(date =&gt; date &gt; now|date_modify('+1 day')|date('Y-m-d')) %}
  
  {% if todaysEvents|length %}    
      &lt;h2&gt;Today’s events&lt;/h2&gt;
      &lt;ul&gt;
          {% for event in todaysEvents[0] %}
              &lt;li&gt;{{ event.eventTitle }}&lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  {% endif %}
  
  {% if tomorrowsEvents|length %}    
      &lt;h2&gt;Tomorrow’s events&lt;/h2&gt;
      &lt;ul&gt;
          {% for event in tomorrowsEvents[0] %}
              &lt;li&gt;{{ event.eventTitle }}&lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  {% endif %}
  
  {% if beyondTomorrowsEvents|length %}
      {% for date, events in beyondTomorrowsEvents %}
          &lt;h2&gt;{{ date }}&lt;/h2&gt;
          {% if events|length %}
              &lt;ul&gt;
                  {% for event in events %}
                      &lt;li&gt;{{ event.eventTitle }}&lt;/li&gt;
                  {% endfor %}
              &lt;/ul&gt;
          {% endif %}
      {% endfor %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-16T08:53:39.477Z
Answers.LastActivityDate: 2024-05-16T08:53:39.477Z
Answers.UserDisplayName: James Smith
Body: <p>I'm trying to group events in a matrix field by day. Each date has a title and start and end time. I would like events for today, tomorrow and any dates beyond grouped separately.</p>
  <p>This is what I have so far but it's not working:</p>
  <pre><code>{% set events = entry.liveStreamEvents %}
  {% set todaysEvents = events|group(&quot;eventStartTime|date('Y')&quot;) %}
                              
  {% for block in todaysEvents %}
      &lt;h2&gt;{{ block.eventTitle }}&lt;/h2&gt;
  {% endfor %}
  </code></pre>
  <p>I get an error <code>Key &quot;eventTitle&quot; for array with keys &quot;0, 1&quot; does not exist.</code></p>
  <p>Where have I gone astray?</p>
  <p>Appreciate any help.</p>
  
Tags: <matrix><datetime><group>
Title: Group matrix blocks by date
ThreadText: # Group matrix blocks by date
  
  (Asked by: username_0 on 2024-05-16)
  
  I'm trying to group events in a matrix field by day. Each date has a title and start and end time. I would like events for today, tomorrow and any dates beyond grouped separately.
  
  This is what I have so far but it's not working:
  
  ```
  {% set events = entry.liveStreamEvents %}
  {% set todaysEvents = events|group("eventStartTime|date('Y')") %}
                              
  {% for block in todaysEvents %}
      <h2>{{ block.eventTitle }}</h2>
  {% endfor %}
  
  ```
  
  I get an error `Key "eventTitle" for array with keys "0, 1" does not exist.`
  
  Where have I gone astray?
  
  Appreciate any help.
  
  **Tags:** matrix, datetime, group
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-05-16. Score: 1
  
  Check out the [documentation for the group filter](https://craftcms.com/docs/5.x/reference/twig/filters.html#group) and you'll realise that it creates a nested array for each thing you want to group by, and the array key of each nested array will be your eventStartTime. Once you have your grouped array you can then cherrypick today's events, tomorrow's events, and the rest using the [filter](https://craftcms.com/docs/5.x/reference/twig/filters.html#filter) filter, combined with Twig's [now](https://twig.symfony.com/doc/3.x/filters/date.html) variable, and the [date\_modify filter](https://twig.symfony.com/doc/3.x/filters/date_modify.html). I haven't tested this but it should work:
  
  ```twig
  {# Fetch all events from the matrix #}
  {% set eventsMatrix = entry.liveStreamEvents.all() %}
  
  {# Group the array by date in the format 2024-12-31 #}
  {% set eventsGroupedByStartDate = eventsMatrix|group("eventStartTime|date('Y-m-d')") %}
  
  {# Filter out today's and tommorrow's events by comparing to `now` #}
  {% set todaysEvents = eventsGroupedByStartDate|filter(date => date == now|date('Y-m-d')) %}
  {% set tomorrowsEvents = eventsGroupedByStartDate|filter(date => date == now|date_modify('+1 day')|date('Y-m-d')) %}
  {% set beyondTomorrowsEvents = eventsGroupedByStartDate|filter(date => date > now|date_modify('+1 day')|date('Y-m-d')) %}
  
  {% if todaysEvents|length %}    
      <h2>Today’s events</h2>
      <ul>
          {% for event in todaysEvents[0] %}
              <li>{{ event.eventTitle }}</li>
          {% endfor %}
      </ul>
  {% endif %}
  
  {% if tomorrowsEvents|length %}    
      <h2>Tomorrow’s events</h2>
      <ul>
          {% for event in tomorrowsEvents[0] %}
              <li>{{ event.eventTitle }}</li>
          {% endfor %}
      </ul>
  {% endif %}
  
  {% if beyondTomorrowsEvents|length %}
      {% for date, events in beyondTomorrowsEvents %}
          <h2>{{ date }}</h2>
          {% if events|length %}
              <ul>
                  {% for event in events %}
                      <li>{{ event.eventTitle }}</li>
                  {% endfor %}
              </ul>
          {% endif %}
      {% endfor %}
  {% endif %}
  
  ```
  
  
  ---
  
  