Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When formatting datetime in twig and printing the timezone
{{ datetime|date('g:i A T') }}
An anonymous user has a null timezone and therefore twig timezone conversion gives unexpected results.
Proposed resolution
The default timezone for anonymous users should be the site's default timezone.
Comments
Comment #2
swentel CreditAttribution: swentel commentedRelated to #2638678: date.timezone not being set in php.ini results in exceptions or #2510150: AccountProxy is not calling date_set_default_timezone() for anonymous users ?
Comment #3
mikemiles86Comment #4
imalabyaCan you give more information regarding the issue?
I tried recreating the issue by printing the timezone in twig file and the time is showing up correctly for anonymous user.
Can you check up what is the `php.ini` time settings for your system? By default it is empty and Drupal takes up UTC as the site timezone.
Comment #5
bdimaggioI commented out the "date.timezone =" line in my php.ini, cleared caches, ensured that page cache was set to something (see #2638678: date.timezone not being set in php.ini results in exceptions) and still wasn't able to reproduce this.
Comment #6
star-szrComment #7
killua99 CreditAttribution: killua99 commentedI did create a field (Date) and try to print the value with twig.
{{ node.field_date_and_time[0].value|date('U')|format_date('custom', 'H:i T', 'Europe/Stockholm') }}
The result was "12:00 CET"
My date.timezone is UTC and Default Timezone is Europe/Stockholm on my php.ini
I'm running without any CacheBackend system.
Also this is happening to the latest 8.1.0-beta2
Comment #8
cilefen CreditAttribution: cilefen commentedCan someone on this issue consider whether this is a Major bug and update accordingly if it is not?
Comment #9
cilefen CreditAttribution: cilefen commentedOh, the issue title refers to working with a user object, but is this a universal problem with dates?
Comment #10
star-szrHopefully this is not hijacking this issue, but…
If I'm interpreting the bug report correctly this might be a universal problem with dates and Twig from my understanding, because our Twig environment doesn't know about Drupal's TZ handling as far as I know. I'm not sure how to solve that but updating the component for now. This doesn't seem major to me because there are definitely workarounds like using preprocess and/or the |format_date Twig filter in core.
One question I have is how do our cache systems (cache contexts etc.) work with different user timezones?
Comment #19
simeUsing the twig syntax from the issue summary (
{{ datetime|date('g:i A T') }}
) I saw the correct date timezone using twig. It reflected:I am marking this unable to reproduce, please reopen if this seems incorrect.