Search code examples
phpsymfonytimezonetwigtwig-extension

Get current timezone in Twig


Is there any way to get current timezone in Twig templates rather than passing from crontroller. This link gives the date for a specific timezone.

{{ post.published_at|date("m/d/Y", "Europe/Paris") }}

Solution

  • Maybe you could try a JS solution, like moment.js. You can assign a special CSS class (eg. date-transform) and run a script on all elements with this class:

    $('.date-transform').each(function(){
            var initialDate = moment($(this).html(), 'MMMM D, YYYY HH:mm');
            if (initialDate.isValid()) {
                var offset = moment().utcOffset(); // this is the client UTC offset
                var finalDate = initialDate.add(offset, 'minutes').format('MMMM D, YYYY HH:mm');
                $(this).html(finalDate);
            }
        });