how do i use timezone in Twig date filter?

2019-04-12 03:45发布

问题:

I am using Twig and this date filter

http://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.

回答1:

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


回答2:

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



回答3:

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



回答4:

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



回答5:

the code that works for me was

/**
originally the $post['created'] contains the value stored in database. 
this value was inserted assuming UTC format.
Best to do something like date_default_timezone_set('UTC'); 
which sets UTC application-wide 
**/

$time = new DateTime($post['created']);
$time->setTimezone(new DateTimeZone('Asia/Singapore')); // if you want Asia/Singapore

// probably the DateTimeZone has to be retrieved from database 
// since it could be user setting
// you can find the list of legit DateTimeZones in
// http://www.php.net/manual/en/timezones.php

$post['created'] = $time;

/**
Then $post is passed to Twig
**/


回答6:

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');
}