Twig: for each month of the Year

It's because twig treats j as number of seconds from January 1970 (so it's always January).

From twig documentation:

The date filter accepts strings (it must be in a format supported by the strtotime function), DateTime instances, or DateInterval instances.

This should work:

{% for j in range(1, 12) %}
    <option>{{ date('2012-' ~ j ~ '-01') |date('M') }}</option>
{% endfor %}

The solution given by Cyprian Throwed me the following error

The function "date" does not exist

So I changed code to

{% for j in 1..12 %}
    <option>{{ j |date('2012-' ~ j ~ '-01') |date('M') }}</option>
{% endfor %}

and this worked for me.... Thanks Cyprian

Tags:

Twig