Is there a way to loop through a day in 15-minute intervals, with the output of said intervals being localizable? This (cringe-worthy) snippet should help explain what I'm trying to accomplish:

    {% for hour in 0..23 %}
        {% for min in [0, 15, 30, 45] %}

            {% set foo = [hour, min]|join(':')|date('H:i')|date_modify('+0 days') %}
            <li>{{ foo.localeTime() }}</li>

        {% endfor %}
    {% endfor %}

For me, that snippet results in the unusual error Method "localeTime" for object "DateTime" does not exist.

(This question was asked on g+ by Chris Davis. I copied it to SE, as I didn't want to answer it there!)

1 Answer 1


You can set the site's timezone in Craft, but you won't get different timezones for your locales with that (locales != timezones btw.). But what you can do is to use Twig's |date filter which allows you to specify a different timezone for the output (list of supported timezones). If you create your own DateTime object by joining strings, you have to add a timezone here to give the filter a basis for its calculations. Make this match your Craft settings.

The following example works without the Localization package.

{# Set locale time to GMT+2 #}
{% set localeTime = "0:00 +0200" %}

        <th>Local Time</th><th>Sao Paulo</th><th>Manaus</th>

    {% for hour in 0..23 %}
        {% for min in [0, 15, 30, 45] %}

                {# Join time segments in a format supported by strtotime #}
                {% set timeStep = ['+ ', hour, ' hours ', min, ' minutes']|join() %}

                {# Modify the locale time and output in chosen timezone #}
                <td>{{ localeTime|date_modify(timeStep)|date('H:i') }}</td>
                <td>{{ localeTime|date_modify(timeStep)|date('H:i', "America/Sao_Paulo") }}</td>
                <td>{{ localeTime|date_modify(timeStep)|date('H:i', "America/Manaus") }}</td>

        {% endfor %}
    {% endfor %}

To bring the site's currently requested locale into the game, just do some comparisons on craft.locale.

{{ craft.locale == 'de-DE' ? localeTime|date('H:i') }}
{{ craft.locale == 'pt-BR' ? localeTime|date('H:i', "America/Sao_Paulo") }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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