This is a site that was recently updated from Craft 2 to Craft 3 and before the update this issue did not occur.

Here is the code:

{# all entries sorted by year/month #}
 {% set allEntries = craft.entries.section('photoblog').limit(null).orderBy('postDate desc').all() %}

  <div class="years">
    {% for year, entriesInYear in allEntries | group("postDate|date('Y')") %}
        <div class="col-sm-3 col-xs-4" data-mh="years">
          <h3>{{ year }}</h3>
          <ul class="list-unstyled">
          {% for month, entries in entriesInYear | group("postDate|date('F')" ) |  reverse %}
              <li><a href="/calendar/{{ year }}/{{ month|date('m') }}">{{ month }}&nbsp;({{ entries|length }})</a></li>
          {% endfor %}
    {% endfor %}

Everything works correctly except for February which always outputs 03 and not 02 as expected.

If you go to https://sean-smith.net/calendar and hover over any February link you'll see that the link is set to March inexplicably.


I think the problem is that you are not being specific enough with the date that you are passing to the date function. You are in effect calling date("February") and the PHP date function has to make an assumption and uses the current day of the month, i.e. February 30 which equates to March. If this is correct you will have even more issues tomorrow being the 31st!

If you make the date being passed more specific i.e. February 1 it should work fine.

A related PHP bug post, that wasn't a bug, explains it https://bugs.php.net/bug.php?id=49115


As @jollyR said in their response there is a bug with php dates. I resolved this issue by changing the relevant code to below.

  {# need month to include year to avoide a bug with short months when the current day is 30 or 31 i.e. february #}
  {% set thisMonth = month ~ '-' ~ year %}

  <a href="/calendar/{{ year }}/{{ thisMonth|date('m') }}">{{ month }}&nbsp;({{ entries|length }})</a>

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.