Resolution
To ensure dates are rendered in the correct regional time zone for different languages in Contao, you must explicitly define the target time zone within the Twig template using the timezone parameter of the date filter. Because Contao does not provide a per-language time zone setting in the admin panel, the application relies on the global PHP date.timezone configuration for all rendered dates by default.
Implementation in Twig
Instead of relying on the server's default offset, pass the desired time zone as the second argument to the date filter. You can map these time zones to your site's language codes:
{# Example: Adjusting timezone based on the current language #}
{% set tz = 'UTC' %}
{% if language == 'de' %}
{% set tz = 'Europe/Berlin' %}
{% elseif language == 'en' %}
{% set tz = 'America/New_York' %}
{% endif %}
{{ date_value|date('d.m.Y H:i', tz) }}
Technical Explanation
Storage vs. Rendering
Contao typically stores timestamps in the database as standard date/time strings. The PHP runtime interprets these based on the date.timezone setting in php.ini. When a DateTime object is created in Twig, it inherits this system default. If your server is set to UTC but your users are in Europe, the rendered time will be offset unless a specific time zone is requested during the formatting process.
CDN and Caching Impact
Global caching (such as Varnish or Cloudflare) can cause "time zone leakage," where a page rendered for a user in one region is cached and served to a user in another. To mitigate this:
- Vary Header: Ensure your server sends a
Vary: Accept-Language header so the CDN caches separate versions of the page for each language.
- Client-Side Rendering: For highly dynamic dates, render the timestamp in ISO 8601 format and use JavaScript (e.g.,
Intl.DateTimeFormat) to convert the time to the user's local browser time zone.
Verification Steps
To diagnose the current configuration, run these checks:
- PHP Runtime: Create a temporary PHP file with
echo date_default_timezone_get(); to confirm the active server offset.
- Database Sync: Run
SELECT NOW(); in your SQL console to see if the database clock matches the system clock.
Diagnostic Detail Needed: Are you using a shared hosting environment where you lack access to php.ini, or do you have full control over the PHP-FPM configuration?