I'm using the technique for creating an archive that's in the Craft docs: https://craftcms.com/support/entry-archive

I'm archiving by day, so my code looks like this:

{% set agendaEntries = craft.entries.section('agenda').status('live,pending,expired').limit(null) %}

{% for day, entriesInDay in agendaEntries | group('postDate.day') %}
  <h2>{{ day | date('Y-m-d') }}</h2>

    {% for entry in entriesInDay %}
          {# entry data #}
    {% endfor %}
{% endfor %}

The looping and grouping (heh) works great, but the {{ day }} variable doesn't correctly output the date, instead I get the day-before-Epoch date (1969-12-31).

I don't really understand how the for loop even knows what the day variable is, so I don't know how to troubleshoot.


Your postDate.day will output the number of the day in the month so if your entries postdate is 02.dd.yyyy your output is 2. Your array index will be something like [2, 3, 5, 12, 31] thus these values will be converted from the timestamp into a readable day with<h2>{{ day | date('Y-m-d') }}</h2>. The first timestamp (0) is 01.01.1970 because of your timezone you'll see only values around this date since you convert all the day numbers from 1-31 as timestamps to your given format.

In case you did not know: the day variable represents only the keys of the array and with | group('postDate.day') you define the keys. I think your goal is to have keys like [ 'Monday', 'Tuesday'.... 'Sunday']. Your filter will look like:

{% for day, entriesInDay in agendaEntries | group('postDate|date('l')') %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.