3

How can I get the current date / time for specific timezones?

My local time is easy:

{{ now|date("H:i") }}

So how can I get this for New York? or Singapore?

4

First define an array of all the timezones you want to output the time with, plus what their “Standard Time”/“Daylight Savings time” abbreviations are:

{% set locales = [
    { timezone: 'Australia/Sydney',    stAbbr: 'AEST', dstAbbr: 'AEDT' },
    { timezone: 'America/Los_Angeles', stAbbr: 'PST',  dstAbbr: 'PDT' },
    { timezone: 'America/New_York',    stAbbr: 'EST',  dstAbbr: 'EDT' },
    { timezone: 'Europe/London',       stAbbr: 'GMT',  dstAbbr: 'BST' },
] %}

Then loop through them, outputting your date in those timezones, plus showing the correct “Standard Time”/“Daylight Savings Time” abbreviation:

{% for locale in locales %}
    {# Output the date/time in this timezone #}
    {{ date()|date('F j ga', locale.timezone) }}

    {# Is that timezone currently observing Standard Time? #}
    {% if date()|date('I', locale.timezone) == '0' %}
        {{ locale.stAbbr }}
    {% else %}
        {{ locale.dstAbbr }}
    {% endif %}
{% endfor %}
  • Gives me the same time for every locale. I'm using {{ now|date("H:i", locale.timezone) }}. So for London the same time as New York. – Johannes Lamers Nov 25 '15 at 17:08
  • @JohannesLamers What happens if you replace now with date()? – Brandon Kelly Nov 25 '15 at 17:28
  • Its working now with {{ date()|date("H:i", locale.timezone) }} – Johannes Lamers Nov 25 '15 at 18:08
  • @JohannesLamers Cool, just updated the code to use date(). – Brandon Kelly Nov 25 '15 at 23:34
4

{{ now|date("H:i", "Europe/Paris") }}

As per: http://twig.sensiolabs.org/doc/filters/date.html

  • Hi, how to do this same in php, i want to fetch entries based on user timezone. – inrsaurabh Jul 23 '19 at 12:45

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.