如何在 Twig 模板中渲染 DateTime 对象
我的实体之一中的字段之一是“日期时间”变量。
如何将此字段转换为字符串以在浏览器中呈现?
这是一个代码片段:
{% for game in games %}
...
<td> {{game.gameTeamIdOne.teamName}} </td>
<td> {{game.gameTeamIdTwo.teamName}} </td>
<td> {{game.gameDate}}</td>
</tr>
{% endfor %}
这是我的实体类中的变量:
/**
* @var date $gameDate
*
* @ORM\Column(name="GAME_DATE", type="datetime", nullable=true)
*/
private $gameDate;
这是我收到的错误消息:
渲染模板期间抛出异常(“可捕获的致命错误:类 DateTime 的对象无法转换为字符串...\app\cache\dev\twig\9b\ad\58fd3bb1517632badf1fdc7fa4a8.php第 33 行”)位于“BeerBundle:Games:gameTable.html.twig”第 10 行。
One of my fields in one of my entities is a "datetime" variable.
How can I convert this field into a string to render in a browser?
Here is a code snippet:
{% for game in games %}
...
<td> {{game.gameTeamIdOne.teamName}} </td>
<td> {{game.gameTeamIdTwo.teamName}} </td>
<td> {{game.gameDate}}</td>
</tr>
{% endfor %}
Here is the variable in my entity class:
/**
* @var date $gameDate
*
* @ORM\Column(name="GAME_DATE", type="datetime", nullable=true)
*/
private $gameDate;
And here is the error message I am getting:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class DateTime could not be converted to string in ...\app\cache\dev\twig\9b\ad\58fd3bb1517632badf1fdc7fa4a8.php line 33") in "BeerBundle:Games:gameTable.html.twig" at line 10.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于包含时区偏移的完整日期时间字符串。
For full date time string including timezone offset.
有一个 symfony2 工具可以显示当前语言环境中的日期:
https://github.com/michelsalib/BCCExtraToolsBundle
There is a symfony2 tool to display date in the current locale:
https://github.com/michelsalib/BCCExtraToolsBundle
您可以通过以下方式渲染
{{ post.published_at|date("m/d/Y") }}
有关更多详细信息,请访问 http://twig.sensiolabs.org/doc/filters/date.html
you can render in following way
{{ post.published_at|date("m/d/Y") }}
For more details can visit http://twig.sensiolabs.org/doc/filters/date.html
您可以使用
twig
的函数日期和三元运算符You can use the function date and
ternary
operator oftwig
新的 format_datetime 过滤器 (2022)
在 twig 3 中,使用 format_datetime 过滤器。
它还支持本地化和自定义模式 (ICU)< /a>.
来源:https://twig.symfony.com/doc/3。 x/filters/format_datetime.html
New format_datetime filter (2022)
In twig 3, use format_datetime filter.
It also supports localization and custom pattern (ICU).
source : https://twig.symfony.com/doc/3.x/filters/format_datetime.html
实际上,sf> v6,树枝 >= 2,
我只是使用
|date
过滤器,例如:如 树枝 v3 文档
actually, sf > v6, twig >= 2,
I just use
|date
filter like :as explain at twig v3 doc
尽管您可以使用该
方法,但请记住,此版本不支持用户区域设置,这对于仅由一种国籍的用户使用的网站来说不应该是问题。国际用户应该以完全不同的方式显示游戏日期,例如扩展
\DateTime
类,并向其添加__toString()
方法来检查区域设置并采取相应的操作。编辑:
正如@Nic在评论中指出的,如果您使用 Twig 的国际扩展,您将拥有一个可用的
localizeddate
过滤器,它显示用户区域设置中的日期。这样你就可以放弃我之前扩展\DateTime
的想法了。Although you can use the
approach, keep in mind that this version does not honor the user locale, which should not be a problem with a site used by only users of one nationality. International users should display the game date totally different, like extending the
\DateTime
class, and adding a__toString()
method to it that checks the locale and acts accordingly.Edit:
As pointed out by @Nic in a comment, if you use the Intl extension of Twig, you will have a
localizeddate
filter available, which shows the date in the user’s locale. This way you can drop my previous idea of extending\DateTime
.您可以使用
date
过滤器:You can use
date
filter:这取决于您希望日期显示的格式。
静态日期格式
如果您想显示静态格式,该格式对于所有语言环境都相同(例如 ISO 8601< /a> 对于 Atom feed),您应该使用 Twig 的
date
过滤器:它将始终返回以下格式的日期时间:
date
过滤器接受的格式字符串与您用于 PHP 的date()
函数。 (唯一的区别是,据我所知,您不能使用可在 PHPdate()
函数中使用的预定义常量)本地化日期(和时间)
但是,由于您如果想要在浏览器中呈现它,您可能希望以人类可读的格式显示它,并针对用户的语言和位置进行本地化。您可以使用国际扩展来实现此目的,而不是自己进行本地化(它利用 PHP 的 IntlDateFormatter)。它提供了一个过滤器
localizeddate
,它将使用本地化格式输出日期和时间。localizeddate
的参数:date_format
:格式字符串(见下文)time_format
:格式字符串(见下文)区域设置
:(可选)使用它来覆盖配置的区域设置。保留此参数以使用默认区域设置,可以在 Symfony 的配置中进行配置。(还有更多,请参阅文档以获取可能参数的完整列表)
对于
date_format
和time_format
,您可以使用以下字符串之一:'none'
(如果您不想包含此元素)'short'
为最缩写的样式(12/13/52 或英语语言环境中的 3:30pm)'medium'
表示中等风格(英语语言环境中的 Jan 12, 1952)'long' 表示长样式(1952 年 1 月 12 日或英语语言环境中的 3:30:32pm)
'full'
表示完全指定的样式(在英语语言环境中,公元 1952 年 4 月 12 日星期二或太平洋标准时间 3:30:42pm)示例
例如,如果您想以相当于 2014 年 2 月 6 日上午 10:52 的格式显示日期,在 Twig 模板中使用以下行:
但是,如果您使用不同的语言环境,结果将针对该语言环境进行本地化:
6 februari 2014 年 10:52
对于nl
语言环境;6 février 2014 10:52
对于fr
语言环境;de
语言环境;如
您所见,
localizeddate
不仅翻译月份名称,还使用本地符号。英语表示法将日期放在月份之后,而荷兰语、法语和德语表示法将日期放在月份之前。英语和德语月份名称以大写字母开头,而荷兰语和法语月份名称以小写字母开头。德国日期附加了一个点。安装/设置区域设置
Intl 扩展的安装说明可以在这个单独的答案中找到。
It depends on the format you want the date to be shown as.
Static date format
If you want to display a static format, which is the same for all locales (for instance ISO 8601 for an Atom feed), you should use Twig's
date
filter:Which will allways return a datetime in the following format:
The format strings accepted by the
date
filter are the same as you would use for PHP'sdate()
function. (the only difference is that, as far as I know, you can't use the predefined constants which can be used in the PHPdate()
function)Localized dates (and times)
However, since you want to render it in the browser, you'll likely want to show it in a human-readable format, localised for the user's language and location. Instead of doing the localization yourself, you can use the Intl Extension for this (which makes use of PHP's IntlDateFormatter). It provides a filter
localizeddate
which will output the date and time using a localized format.Arguments for
localizeddate
:date_format
: One of the format strings (see below)time_format
: One of the format strings (see below)locale
: (optional) Use this to override the configured locale. Leave this argument out to use the default locale, which can be configured in Symfony's configuration.(there are more, see the docs for the complete list of possible arguments)
For
date_format
andtime_format
you can use one of the following strings:'none'
if you don't want to include this element'short'
for the most abbreviated style (12/13/52 or 3:30pm in an English locale)'medium'
for the medium style (Jan 12, 1952 in an English locale)'long'
for the long style (January 12, 1952 or 3:30:32pm in an English locale)'full'
for the completely specified style (Tuesday, April 12, 1952 AD or 3:30:42pm PST in an English locale)Example
So, for instance, if you want to display the date in a format equivalent to
February 6, 2014 at 10:52 AM
, use the following line in your Twig template:However, if you use a different locale, the result will be localized for that locale:
6 februari 2014 10:52
for thenl
locale;6 février 2014 10:52
for thefr
locale;6. Februar 2014 10:52
for thede
locale;etc.
As you can see,
localizeddate
does not only translate the month names but also uses the local notations. The English notation puts the date after the month, where Dutch, French and German notations put it before the month. English and German month names start with an uppercase letter, whereas Dutch and French month names are lowercase. And German dates have a dot appended.Installation / setting the locale
Installation instructions for the Intl extension can be found in this seperate answer.
我知道这是一个很老的问题,但我今天发现了这个问题,但答案不是我需要的。
这就是我所需要的。
如果您像我一样希望在 twig 中显示当前日期,则可以使用以下内容:
{{ "now"|date("m/ d/Y") }}
请参阅有关此内容的文档:
日期在树枝中
I know this is a pretty old question, but I found this question today, but the answers were not what I needed.
So here's what I needed.
If you, like me, are looking to display the current date in twig, you can use the following:
{{ "now"|date("m/d/Y") }}
See documentation about this:
date in twig
为了避免空值错误,您可以使用以下代码:
To avoid error on null value you can use this code:
不要忘记
实体:
视图:
>>输出 2013-09-18 16:14:20
Dont forget
Entity :
View:
>> Output 2013-09-18 16:14:20