---
title: Date Bug
date: 2018-12-31T09:22:00-05:00
author: cc_admin
canonical_url: "https://caffeinecreations.ca/blog/date-bug/"
section: Blog
---
![Bug](https://caffeinecreations.ca/uploads/blog/_1920x660_crop_center-center_none_ns/bug.jpg)

- [Code](https://caffeinecreations.ca/blog/category/code/), [CraftCMS](https://caffeinecreations.ca/blog/category/craftcms/), [Craft Tips](https://caffeinecreations.ca/blog/category/craftcms/craft-tips/)

# Date Bug

I recently updated my [photo blog](http://sean-smith.net) (desperately needs a redesign) from Craft 2.x to Craft 3.0.36 (as of writing this blog post). Yesterday I noticed that the archives template had an issue when trying to view February in any year. It would, inexplicably, take me to March.

The code that was causing this issue is:

```
<button class="absolute z-10 flex items-center justify-center w-8 h-8 -translate-y-1/2  -right-4 -top-4" clipboard="" title="Copy to Clipboard" to="" type="button" x-clipboard.raw="{# 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 %}
          </ul>
        </div>
    {% endfor %}
  </div>" x-data="">
	<svg class="h-5 w-5" viewbox="0 0 64 64" xml:space="preserve" xmlns="http://www.w3.org/2000/svg">
  <rect fill="#f3f4f6" height="53" rx="3" width="41" x="7" y="2"></rect>
  <rect fill="#f3f4f6" height="51" rx="3" width="39" x="19" y="11"></rect>
  <path d="M53.98 9.143h-3.97c-.082 0-.155.028-.232.047V5.023C49.778 2.253 47.473 0 44.64 0H10.217C7.384 0 5.08 2.253 5.08 5.023v46.843c0 2.77 2.305 5.023 5.138 5.023h6.037v2.268c0 2.67 2.216 4.843 4.941 4.843H53.98c2.725 0 4.942-2.173 4.942-4.843v-45.17c0-2.671-2.217-4.844-4.942-4.844zM7.11 51.866V5.023c0-1.649 1.394-2.991 3.106-2.991H44.64c1.712 0 3.106 1.342 3.106 2.99v46.844c0 1.649-1.394 2.991-3.106 2.991H10.217c-1.712 0-3.106-1.342-3.106-2.99zm49.778 7.29c0 1.551-1.306 2.812-2.91 2.812H21.195c-1.604 0-2.91-1.26-2.91-2.811v-2.268H44.64c2.833 0 5.138-2.253 5.138-5.023V11.128c.077.018.15.047.233.047h3.968c1.604 0 2.91 1.26 2.91 2.811v45.17z"></path>
  <path d="M38.603 13.206H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 21.333H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 29.46H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM28.444 37.587h-12.19a1.015 1.015 0 1 0 0 2.032h12.19a1.015 1.015 0 1 0 0-2.032z"></path>
</svg>
<div class="sr-only">Copy to clipboard</div></button>```twig
{# 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 %}
          </ul>
        </div>
    {% endfor %}
  </div>
```
```

Specifically this bit `{{ month|date('m') }}` is subject to a [php bug](http://bugs.php.net/bug.php?id=49115) when the current day is a number higher than the month being viewed has. Yesterday was December 30th and the 30th does not exist in February.

Since twig is just a wrapper for php code that means that twig dates are subject to the same bug.

The solution to this is to simply add the year to the date which then according to the bug linked above forces the month to use the first day of the month and not the current day. And the working code is here:

```
<button class="absolute z-10 flex items-center justify-center w-8 h-8 -translate-y-1/2  -right-4 -top-4" clipboard="" title="Copy to Clipboard" to="" type="button" x-clipboard.raw="<li>
  {# 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>
</li>" x-data="">
	<svg class="h-5 w-5" viewbox="0 0 64 64" xml:space="preserve" xmlns="http://www.w3.org/2000/svg">
  <rect fill="#f3f4f6" height="53" rx="3" width="41" x="7" y="2"></rect>
  <rect fill="#f3f4f6" height="51" rx="3" width="39" x="19" y="11"></rect>
  <path d="M53.98 9.143h-3.97c-.082 0-.155.028-.232.047V5.023C49.778 2.253 47.473 0 44.64 0H10.217C7.384 0 5.08 2.253 5.08 5.023v46.843c0 2.77 2.305 5.023 5.138 5.023h6.037v2.268c0 2.67 2.216 4.843 4.941 4.843H53.98c2.725 0 4.942-2.173 4.942-4.843v-45.17c0-2.671-2.217-4.844-4.942-4.844zM7.11 51.866V5.023c0-1.649 1.394-2.991 3.106-2.991H44.64c1.712 0 3.106 1.342 3.106 2.99v46.844c0 1.649-1.394 2.991-3.106 2.991H10.217c-1.712 0-3.106-1.342-3.106-2.99zm49.778 7.29c0 1.551-1.306 2.812-2.91 2.812H21.195c-1.604 0-2.91-1.26-2.91-2.811v-2.268H44.64c2.833 0 5.138-2.253 5.138-5.023V11.128c.077.018.15.047.233.047h3.968c1.604 0 2.91 1.26 2.91 2.811v45.17z"></path>
  <path d="M38.603 13.206H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 21.333H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 29.46H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM28.444 37.587h-12.19a1.015 1.015 0 1 0 0 2.032h12.19a1.015 1.015 0 1 0 0-2.032z"></path>
</svg>
<div class="sr-only">Copy to clipboard</div></button>```twig
<li>
  {# 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>
</li>
```
```

Now all my links are working correctly. Honestly I"m surprised that php hasn't fixed this bug in the last 9.5 years but at least there's an easy fix. Got pointed in the right direction on this [Stack Exchange thread](http://craftcms.stackexchange.com/questions/28972/february-always-comes-out-as-march-expect-yyyy-mm-to-be-2018-02-but-its-2018).

## Other Articles of Interest

[![News: Website 101 Podcast Thumbnail](https://caffeinecreations.ca/uploads/hero/_680x320_crop_center-center_65_none_ns/website101-hero1.jpg)### News: Website 101 Podcast](https://caffeinecreations.ca/blog/launching-website-101-podcast/)

[![SVG Sprites and Twig Macros in Craft CMS Thumbnail](https://caffeinecreations.ca/uploads/hero/_680x320_crop_center-center_65_none_ns/rainbow-stones.jpg)### SVG Sprites and Twig Macros in Craft CMS](https://caffeinecreations.ca/blog/svg-sprites-and-twig-macros-in-craft-cms/)

[![The Dangers of Over Reliance on Plugins in Website Builds Thumbnail](https://caffeinecreations.ca/uploads/hero/_680x320_crop_center-center_65_none_ns/gears.jpeg)### The Dangers of Over Reliance on Plugins in Website Builds](https://caffeinecreations.ca/blog/the-dangers-of-over-reliance-on-plugins-in-website-builds/)
