3

In my user profiles, I have a text field where the user's timezone is set, in the standard America/New_York format.

Craft's timezone is set to New York, and the server's timezone is set to UTC.

I'm trying to use the Twig date filter to localize the times using the user profile timezone field, but it doesn't seem to be working.

For example:

{{ message.postDate | date('n/d/Y g:ia') }} outputs 12/11/2016 1:00pm

I'm hoping that for a user in the pacific timezone, this time will display as 10:00am. But this is what happens:

{{ currentUser.userTimezone }} outputs America/Los_Angeles

{{ message.postDate | date('n/d/Y g:ia', currentUser.userTimezone) }} still outputs 12/11/2016 1:00pm

I've also tried just hard-coding a different timezone in, to no avail:

{{ message.postDate | date('n/d/Y g:ia', 'America/Los_Angeles') }} still outputs 12/11/2016 1:00pm

Am I doing something wrong? Or is there a different way to do this?

4

It is possible to work around the Craft 2 bug, by converting the Craft DateTime object to a native one.

{% set craftDateTime = entry.postDate %}
{% set nativeDateTime = date(craftDateTime|date('c')) %}

{{ nativeDateTime|date('n/d/Y g:ia', 'America/Los_Angeles') }}
2

This is a known Craft 2 bug in our overridden implementation of PHP's DateTime class and how Twig interacts with it.

It should be fixed before the Craft 3 beta launch because it is on our list to stop overriding that class and rely on PHP's native functionality.

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.