开发者

how do i use timezone in Twig date filter?

开发者 https://www.devze.com 2023-02-21 04:26 出处:网络
I am using Twig and this date filter http:/开发者_Python百科/www.twig-project.org/doc/templates.html#date

I am using Twig and this date filter

http:/开发者_Python百科/www.twig-project.org/doc/templates.html#date

Apparently they are looking out for DateTime instances in the parameter.

looking at this http://www.php.net/manual/en/datetime.construct.php

I have trouble understanding the php datetime object and how to use the timezone.

Given that i know basic PHP and is familiar with simple web programming, how do I use it to display a date and time using the Twig date filter while catering for timezone?

If there is a simpler way to do it while using the date filter, but NOT using datetime object, i would be open to it.

I am only concerned that the solution works, rather than the "correctness" or "elegance" of the solution.


The "Date" filter of Twig accept a second parameter: "timezone".

So, you can easily display all timezone that you want. For example:

{{ "now"|date("m/d/Y H:i", "Europe/Paris") }}
{{ "now"|date("m/d/Y H:i", "Asia/Calcutta") }}
{{ "now"|date("m/d/Y H:i", "Europe/Berlin") }}

For more informations: http://twig.sensiolabs.org/doc/filters/date.html#timezone


In today's version, it has been supported in symfony application config file:

twig:
    date:
        timezone: Asia/Tokyo

http://symfony.com/blog/new-in-symfony-2-7-default-date-and-number-format-configuration


i know the question is old, but this is for reference. By default Twig is going to use the default timezone that is set in php ini file or in the application globally, or the declared in twig.

if you pass a datetime object to the date filter with timezone , then you can pass false as a second argument in date filter.

{{ dateForSomething | date('theFormatIWant', false) }}

please refer to documentation twig date


I think you might have misread the documentation.

The date filter accepts any date format supported by DateTime and DateTime instances.

That means that you can just pass in things like "2011-01-20 12:00:00" OR a real DateTime Object.

But you don't have to deal with the object if you don't want do.

Now if you need that string to be displayed in a specifiy timezone I would set that timezone in php before passing it to twig

$x = new DateTime("2010-01-01 12:00:00");
$x->setTimezone(new DateTimeZone("The Timezone you need"));
// pass to twig


What worked for me is adding a new filter, so it always looks at the timezone within the DateTime obj. Below the example with the DateTime object as a parameter.

I may be missing something by I don't understand why Twig is ignoring the DateTime Timezone part and using the default global one.

In the filters extension:

public function getFilters()
{
    return array(
    (...)
    new \Twig_SimpleFilter('date_tz', array($this, 'dateTzFilter')),
    );
}

and

public function dateTzFilter(\DateTime $dateTime)
{
    return $datTime->format('desired_format');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消