如何在Twig日期过滤器中使用时区? [英] how do i use timezone in Twig date filter?
问题描述
我正在使用Twig和此日期过滤器
I am using Twig and this date filter
http://www.twig-project.org/doc/templates.html#date
显然他们正在寻找对于参数中的DateTime实例。
Apparently they are looking out for DateTime instances in the parameter.
查看这个 http://www.php.net/manual/en/datetime.construct.php
我无法理解php datetime对象以及如何使用时区。
I have trouble understanding the php datetime object and how to use the timezone.
鉴于我知道基本的PHP,熟悉简单的Web编程,如何使用它来显示日期和时间使用Twig日期过滤器,同时适应时区?
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?
如果在使用日期过滤器时使用更简单的方法,但不使用datetime对象,我将可以
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.
推荐答案
我想你可能误读了这些文档。
I think you might have misread the documentation.
日期过滤器接受DateTime和DateTime实例支持的任何日期格式。
The date filter accepts any date format supported by DateTime and DateTime instances.
这意味着你可以通过在 2011-01-20 12:00:00
或一个真正的DateTime对象。
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.
现在如果您需要该字符串要显示在指定的时区,我会将该时区设置为php,然后将其传递给twig
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
这篇关于如何在Twig日期过滤器中使用时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!