<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>

CC BY-SA 4.0
2024-05-16T08:53:39.477Z
2024-05-16T08:53:39.477Z
James Smith
<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>

<matrix><datetime><group>
Group matrix blocks by date
# 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 %}
    {{ block.eventTitle }}
{% 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 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 filter, combined with Twig's now variable, and the date_modify filter. I haven't tested this but it should work:
{# 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 %}    
    Today’s events
    
        {% for event in todaysEvents[0] %}
            {{ event.eventTitle }}
        {% endfor %}
    
{% endif %}

{% if tomorrowsEvents|length %}    
    Tomorrow’s events
    
        {% for event in tomorrowsEvents[0] %}
            {{ event.eventTitle }}
        {% endfor %}
    
{% endif %}

{% if beyondTomorrowsEvents|length %}
    {% for date, events in beyondTomorrowsEvents %}
        {{ date }}
        {% if events|length %}
            
                {% for event in events %}
                    {{ event.eventTitle }}
                {% endfor %}
            
        {% endif %}
    {% endfor %}
{% endif %}


---

