2

I can't figure out what I'm doing wrong, but trying to convert single digit months to their name representations (ex. '4' to 'April' in English) but am just getting December printed out for every single one. I can't compare to month names because the site is in quite a few languages, so need to actually use the date functionality.

{% set groupedEntries = entriesInYear | group("postDate|date('n')") %}

{% for key, eachMonth in groupedEntries %}
    {% set monthName = date(key) | date('F') %}
    <li>
        <a href="{{ craft.languageLink.url(craft.locale, urlParam) }}"
            {{ monthName }}, {{ eachMonth|length }}
        </a>
    </li>
{% endfor %}

'Key' is the month representation with no leading 0's (4, 3, and 2 for posts that exist in April, March and February).

Getting output as: December, 4 December, 4 December, 2 (following number is number of posts in that month)

WHAT the heck am I doing wrong?

2

You can't create a date from a single digit. This is what I would do instead:

  • Get the localized names from Craft's data set with this function:

    {% set monthName = craft.i18n.getLocaleData().getMonthName(key)|date('F') %}
    

    There some info about that craft.i18n.getLocaleData in a Craft help article: "How do I access locale data from my templates?"

  • Create a placeholder date:

    {% set monthName = date('2000-' ~ key ~ '-01')|date('F') %}
    

    To make this work you have to adjust the parameter in the group filter to use leading zero with the month numbers:

    {% set groupedEntries = entriesInYear|group("postDate|date('m')") %}
    
  • Beat me to it! Great answer though. :) Couple of minor issues... key represents month, which should be in the middle. Converting the single-digit month to leading zeroes might be a little complicated (depending on her other logic). And translating the month name could be as simple as |t. Other than that though, you totally nailed it. ;) – Lindsey D Aug 28 '15 at 18:59
  • Oups... Year-Month-Day. – carlcs Aug 28 '15 at 19:23
  • The craft.i18n.getLocaleData() version was throwing errors in other languages, but using {% set monthName = date('2000-' ~ key ~ '-01')|date('F') %} worked like a charm! Thanks so much! – megkadams Aug 28 '15 at 20:04
3

Here's what's happening... Every time you get "December", it's because the date is being parsed as December 31, 1969 (aka, just before the Unix Epoch). Basically, it's failing to parse the date properly, and thus defaulting back to the Epoch.

And here's why... date(key) is basically saying "Get the date of 4". Which obviously doesn't make any sense. So the "date of 4" is a fail, and you get the Epoch default instead.

So here's what I recommend instead... replace this:

{% set monthName = date(key) | date('F') %}

... with something more like this:

{% switch key %}
    {% case 1 %}
        {% set monthName = 'January'|t %}
    {% case 2 %}
        {% set monthName = 'February'|t %}
    {% case 3 %}
        {% set monthName = 'March'|t %}
    {% case 4 %}
        {% set monthName = 'April'|t %}
    {% case 5 %}
        {% set monthName = 'May'|t %}
    {% case 6 %}
        {% set monthName = 'June'|t %}
    {% case 7 %}
        {% set monthName = 'July'|t %}
    {% case 8 %}
        {% set monthName = 'August'|t %}
    {% case 9 %}
        {% set monthName = 'September'|t %}
    {% case 10 %}
        {% set monthName = 'October'|t %}
    {% case 11 %}
        {% set monthName = 'November'|t %}
    {% case 12 %}
        {% set monthName = 'December'|t %}
{% endswitch %}

That's a big chunk of code, I know. So feel free to offload that to a macro if it's more convenient.

Alternately, you could use a placeholder date, as carlcs suggested in his answer.

  • 2
    Awesome! And thanks for the explanation/history lesson, I can't wait for someone to ask me something similar so I can pull out that little factoid. :) – megkadams Aug 28 '15 at 20:02

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.