<p>This is the solution I ended up with. Creating arrays of both:</p>

<ul>
<li>The days in the week from and including today</li>
<li>The days in the week previous to today</li>
</ul>

<p>And then looping through each in succession to produce the format I want. I also added some code to display "today" and "tomorrow" as the title.</p>

<p>I have no doubt there will be a more elegant solution but this did the job for me.</p>

<pre><code>{% for entry in craft.entries.section('menuSection').find() %}

    &lt;ul&gt;

        {% set day = now|date('l') %}

        {% set daysFromToday = [] %}
        {% set previousDays = [] %}

        {% for item in menuItem.weeklyMenuItems %}
            {% if item.dayOfTheWeek == day %}
                {% set daysFromToday = daysFromToday|merge([item.id]) %}
                {% set day = day|date_modify('+1 day') %}
                {% set day = day|date('l') %}
            {% else %}
                {% set previousDays = previousDays|merge([item.id])  %}
            {% endif %}
        {% endfor %}

        {% set daysFromTodayString = daysFromToday|join(', ') %}
        {% set previousDaysString = previousDays|join(', ')  %}

        {% for item in menuItem.weeklyMenuItems.id(daysFromTodayString) %}

            &lt;li&gt;
                &lt;h3&gt;{% if loop.index == 1 %}today{% elseif loop.index == 2 %}Tomorrow{% else %}{{ item.dayOfTheWeek }}{% endif %}&lt;/h3&gt;
                &lt;p&gt;{{ item.itemTitle }}&lt;/p&gt;
            &lt;/li&gt;

        {% endfor %}

        {% for item in menuItem.weeklyMenuItems.id(previousDaysString) %}

            &lt;li&gt;
                &lt;div&gt;
                    &lt;h3&gt;{{ item.dayOfTheWeek }}&lt;/h3&gt;
                    &lt;p&gt;{{ item.itemTitle }}&lt;/p&gt;
                &lt;/div&gt;
            &lt;/li&gt;

        {% endfor %}
    &lt;/ul&gt;

{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-10-04T07:22:47.830Z
2017-10-04T07:22:47.830Z
MikeeBee
<p>Do you really want to start the Matrix loop from the week day offset?  Or do you just want to only start printing when that offset matches.  The former seems like a bit of a micro-optimization.</p>

<p>For the latter, something like this completely untested code should do it:</p>

<pre><code>{% for entry in craft.entries.section('menuSection').find() %}

    {% for menuItem in entry.menuEntries %}

        &lt;h2&gt;{{ menuItem.title }}&lt;/h2&gt;

        &lt;ul&gt;
            {% set offset = false %}

            {# Matrix blocks #}
            {% for item in menuItem.weeklyMenuItems %}

                {% if item.dayOfTheWeek == now|date('l') or offset &gt; 0 }}
                    {% set offset = loop.index0 %}

                    &lt;li&gt;
                        &lt;h3&gt;{{ item.dayOfTheWeek }} - Matched&lt;/h3&gt;
                        &lt;p&gt;{{ item.itemTitle }} - Matched&lt;/p&gt;
                    &lt;/li&gt;
                {% endif %}
            {% endfor %}

            {% if offset == false %}
                {% set offset = 0 %}
            {% endif %}

            {# 2nd pass #}
            {% for item in menuItem.weeklyMenuItems|slice(0, offset) %}
                &lt;li&gt;
                    &lt;h3&gt;{{ item.dayOfTheWeek }} - Matched&lt;/h3&gt;
                    &lt;p&gt;{{ item.itemTitle }} - Matched&lt;/p&gt;
                &lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;

    {% endfor %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-01-11T07:31:05.610Z
I would like to start the Matrix loop from todays day, but still include previous days. To give you a little more context, this is a carousel showing menu items. So I still need the user to be able to see previous days items (as they are the same for months at a time). Thanks!
MikeeBee
CC BY-SA 3.0
2017-01-11T23:23:26.370Z
Updated answer.
Brad Bell
CC BY-SA 3.0
2017-01-14T01:48:54.357Z
Thanks Brad, that will mark the one that is todays date but doesn't change the print order. The list still starts from Monday, regardless. Would you be able to show me how to change the order please?
MikeeBee
CC BY-SA 3.0
2017-01-15T04:32:48.253Z
Sorry, not following.  Maybe edit the original question to show the output results you're looking for?
Brad Bell
CC BY-SA 3.0
2017-01-15T07:47:57.523Z
Edited, hopefully that clarifies things.
MikeeBee
CC BY-SA 3.0
2017-01-15T18:12:57.883Z
Updated answer.
Brad Bell
CC BY-SA 3.0
2017-01-16T07:59:02.690Z
I'm not sure what is happening but, with this, if todays day is Sunday, it works perfectly.

If todays day is Monday it only shows that day. If todays day is Tuesday it shows...

Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday (loops again 5 additional days)

If todays day is Wednesday it starts where it should but carries on past where it should for 4 extra days. 

If todays day is Thursday it starts where it should but carries on past where it should for 3 additional days. 

Can you see what is causing that pattern?
MikeeBee
CC BY-SA 3.0
2017-01-22T01:40:19.083Z
Any ideas on what the issue is?
MikeeBee
CC BY-SA 3.0
2017-01-10T00:24:16.683Z
2017-01-15T18:12:48.093Z
2017-01-15T18:12:48.093Z
Brad Bell
<p>I have a Matrix block <code>weeklyMenuItems</code>, each item in the block has a dropdown with days of the week (Monday, Tuesday etc). Each entry will have a Matrix block with 7 items, matching the 7 days of the week.</p>

<p>In the template, I would like to start the loop from todays day <strong>but still include previous days in the loop, at the end</strong>. So, according to the example, whichever <code>item.dayOfTheWeek</code> matches <code>{{ now|date('l') }}</code>.</p>

<p>For instance, if today's day is <strong>Wednesday</strong>, the loop will output...</p>

<ul>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
<li>Saturday</li>
<li>Sunday</li>
<li>Monday</li>
<li>Tuesday</li>
</ul>

<p>Here is part of my code, the entries that are being looped are from an entries field of another channel <code>menuSection</code> but I don't think that matters in this instance.</p>

<pre><code>{% for entry in craft.entries.section('menuSection').find() %}

    {% for menuItem in entry.menuEntries %}

        &lt;h2&gt;{{ menuItem.title }}&lt;/h2&gt;

        &lt;ul&gt;
            {# Matrix blocks #}
            {% for item in menuItem.weeklyMenuItems %}
                &lt;li&gt;
                    &lt;h3&gt;{{ item.dayOfTheWeek }}&lt;/h3&gt;
                    &lt;p&gt;{{ item.itemTitle }}&lt;/p&gt;
                &lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;

    {% endfor %}

{% endfor %}
</code></pre>

<p>Any guidance will be welcomed, let me know if I can clarify anything!</p>

<p>Thanks</p>

<matrix><datetime>
Start Matrix loop from item that contains todays day
# Start Matrix loop from item that contains todays day

(Asked by: username_0 on 2017-01-09)

I have a Matrix block weeklyMenuItems, each item in the block has a dropdown with days of the week (Monday, Tuesday etc). Each entry will have a Matrix block with 7 items, matching the 7 days of the week.
In the template, I would like to start the loop from todays day but still include previous days in the loop, at the end. So, according to the example, whichever item.dayOfTheWeek matches {{ now|date('l') }}.
For instance, if today's day is Wednesday, the loop will output...

Wednesday
Thursday
Friday
Saturday
Sunday
Monday
Tuesday

Here is part of my code, the entries that are being looped are from an entries field of another channel menuSection but I don't think that matters in this instance.
{% for entry in craft.entries.section('menuSection').find() %}

    {% for menuItem in entry.menuEntries %}

        {{ menuItem.title }}

        
            {# Matrix blocks #}
            {% for item in menuItem.weeklyMenuItems %}
                
                    {{ item.dayOfTheWeek }}
                    {{ item.itemTitle }}
                
            {% endfor %}
        

    {% endfor %}

{% endfor %}

Any guidance will be welcomed, let me know if I can clarify anything!
Thanks

**Tags:** matrix, datetime


---

## 2 answers

### Answer by username_0 on 2017-10-04. Score: 1 | Accepted answer

This is the solution I ended up with. Creating arrays of both:

The days in the week from and including today
The days in the week previous to today

And then looping through each in succession to produce the format I want. I also added some code to display "today" and "tomorrow" as the title.
I have no doubt there will be a more elegant solution but this did the job for me.
{% for entry in craft.entries.section('menuSection').find() %}

    

        {% set day = now|date('l') %}

        {% set daysFromToday = [] %}
        {% set previousDays = [] %}

        {% for item in menuItem.weeklyMenuItems %}
            {% if item.dayOfTheWeek == day %}
                {% set daysFromToday = daysFromToday|merge([item.id]) %}
                {% set day = day|date_modify('+1 day') %}
                {% set day = day|date('l') %}
            {% else %}
                {% set previousDays = previousDays|merge([item.id])  %}
            {% endif %}
        {% endfor %}

        {% set daysFromTodayString = daysFromToday|join(', ') %}
        {% set previousDaysString = previousDays|join(', ')  %}

        {% for item in menuItem.weeklyMenuItems.id(daysFromTodayString) %}

            
                {% if loop.index == 1 %}today{% elseif loop.index == 2 %}Tomorrow{% else %}{{ item.dayOfTheWeek }}{% endif %}
                {{ item.itemTitle }}
            

        {% endfor %}

        {% for item in menuItem.weeklyMenuItems.id(previousDaysString) %}

            
                
                    {{ item.dayOfTheWeek }}
                    {{ item.itemTitle }}
                
            

        {% endfor %}
    

{% endfor %}


---

### Answer by username_1 on 2017-01-10. Score: 1

Do you really want to start the Matrix loop from the week day offset?  Or do you just want to only start printing when that offset matches.  The former seems like a bit of a micro-optimization.
For the latter, something like this completely untested code should do it:
{% for entry in craft.entries.section('menuSection').find() %}

    {% for menuItem in entry.menuEntries %}

        {{ menuItem.title }}

        
            {% set offset = false %}

            {# Matrix blocks #}
            {% for item in menuItem.weeklyMenuItems %}

                {% if item.dayOfTheWeek == now|date('l') or offset > 0 }}
                    {% set offset = loop.index0 %}

                    
                        {{ item.dayOfTheWeek }} - Matched
                        {{ item.itemTitle }} - Matched
                    
                {% endif %}
            {% endfor %}

            {% if offset == false %}
                {% set offset = 0 %}
            {% endif %}

            {# 2nd pass #}
            {% for item in menuItem.weeklyMenuItems|slice(0, offset) %}
                
                    {{ item.dayOfTheWeek }} - Matched
                    {{ item.itemTitle }} - Matched
                
            {% endfor %}
        

    {% endfor %}

{% endfor %}

> **Comments:**
> **username_0**: I would like to start the Matrix loop from todays day, but still include previous days. To give you a little more context, this is a carousel showing menu items. So I still need the user to be able to see previous days items (as they are the same for months at a time). Thanks!  
> **username_1**: Updated answer.  
> **username_0**: Thanks Brad, that will mark the one that is todays date but doesn't change the print order. The list still starts from Monday, regardless. Would you be able to show me how to change the order please?  
> **username_1**: Sorry, not following.  Maybe edit the original question to show the output results you're looking for?  
> **username_0**: Edited, hopefully that clarifies things.  
> **username_1**: Updated answer.  
> **username_0**: I'm not sure what is happening but, with this, if todays day is Sunday, it works perfectly.  If todays day is Monday it only shows that day. If todays day is Tuesday it shows...  Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday (loops again 5 additional days)  If todays day is Wednesday it starts where it should but carries on past where it should for 4 extra days.   If todays day is Thursday it starts where it should but carries on past where it should for 3 additional days.   Can you see what is causing that pattern?  
> **username_0**: Any ideas on what the issue is?  

---

