php 如何在 Twig 模板中呈现 DateTime 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8318914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render a DateTime object in a Twig template
提问by Chris Ridmann
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.
模板渲染过程中抛出异常(“Catchable Fatal Error: Object of class DateTime could not be convert to string in ...\app\cache\dev\twig\9b\ad\58fd3bb1517632badf1fdc7fa4a8.php line 33” ) 在第 10 行的“BeerBundle:Games:gameTable.html.twig”中。
回答by GergelyPolonkai
Although you can use the
虽然您可以使用
{{ game.gameDate|date('Y-m-d') }}
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.
方法,请记住,此版本不尊重用户区域设置,这对于仅由一个国籍的用户使用的站点应该不是问题。国际用户应该显示完全不同的游戏日期,比如扩展\DateTime
类,并添加一个__toString()
方法来检查区域设置并相应地采取行动。
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
.
正如@Nic 在评论中指出的那样,如果您使用Twig的Intl 扩展名,您将有一个localizeddate
可用的过滤器,它显示用户语言环境中的日期。这样你就可以放弃我之前扩展\DateTime
.
回答by Nic Wortel
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 8601for an Atom feed), you should use Twig's date
filter:
如果要显示所有语言环境都相同的静态格式(例如Atom 提要的ISO 8601),则应使用 Twig 的date
过滤器:
{{ game.gameDate|date('Y-m-d\TH:i:sP') }}
Which will allways return a datetime in the following format:
这将始终以以下格式返回日期时间:
2014-05-02T08:55:41Z
2014-05-02T08:55:41Z
The format strings accepted by the date
filter are the same as you would use for PHP's date()
function. (the only difference is that, as far as I know, you can't use the predefined constants which can be used in the PHP date()
function)
date
过滤器接受的格式字符串与您用于PHPdate()
函数的格式字符串相同。(唯一的区别是,据我所知,您不能使用可以在 PHPdate()
函数中使用的预定义常量)
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 Extensionfor this (which makes use of PHP's IntlDateFormatter). It provides a filter localizeddate
which will output the date and time using a localized format.
但是,由于您希望在浏览器中呈现它,您可能希望以人类可读的格式显示它,并针对用户的语言和位置进行了本地化。您可以为此使用Intl Extension(它利用 PHP 的IntlDateFormatter),而不是自己进行本地化。它提供了一个过滤器localizeddate
,将使用本地化格式输出日期和时间。
localizeddate( date_format, time_format [, locale ] )
Arguments for localizeddate
:
的论据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.
date_format
:格式字符串之一(见下文)time_format
:格式字符串之一(见下文)locale
:(可选)使用它来覆盖配置的语言环境。保留此参数以使用默认语言环境,可以在 Symfony 的配置中进行配置。
(there are more, see the docsfor the complete list of possible arguments)
(还有更多,有关可能参数的完整列表,请参阅文档)
For date_format
and time_format
you can use one of the following strings:
对于date_format
和time_format
您可以使用下列字符串之一:
'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)
'none'
如果你不想包含这个元素'short'
对于最简洁的样式(英语语言环境中的 12/13/52 或下午 3:30)'medium'
中等风格(1952 年 1 月 12 日,英语语言环境)'long'
对于长样式(1952 年 1 月 12 日或英语语言环境中的下午 3:30:32)'full'
对于完全指定的样式(公元 1952 年 4 月 12 日星期二或太平洋标准时间下午 3:30:42 在英语语言环境中)
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:
因此,例如,如果要以等效于 的格式显示日期,请February 6, 2014 at 10:52 AM
在 Twig 模板中使用以下行:
{{ game.gameDate|localizeddate('long', 'short') }}
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.
6 februari 2014 10:52
为nl
当地;6 février 2014 10:52
为fr
当地;6. Februar 2014 10:52
为de
当地; 等等。
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.
如您所见,localizeddate
不仅翻译月份名称,还使用本地符号。英语符号将日期放在月份之后,而荷兰语、法语和德语符号则将日期放在月份之前。英语和德语月份名称以大写字母开头,而荷兰语和法语月份名称以小写字母开头。德国日期附加了一个点。
Installation / setting the locale
安装/设置语言环境
Installation instructions for the Intl extension can be found in this seperate answer.
Intl 扩展的安装说明可以在这个单独的答案中找到。
回答by Ivan
Dont forget
不要忘记
@ORM\HasLifecycleCallbacks()
@ORM\HasLifecycleCallbacks()
Entity :
实体 :
/**
* Set gameDate
*
* @ORM\PrePersist
*/
public function setGameDate()
{
$this->dateCreated = new \DateTime();
return $this;
}
View:
看法:
{{ item.gameDate|date('Y-m-d H:i:s') }}
{{ item.gameDate|date('Y-m-d H:i:s') }}
>> Output 2013-09-18 16:14:20
>> 输出 2013-09-18 16:14:20
回答by Venkat Kotra
{{game.gameDate | date('c')}} // 2014-02-05T16:45:22+00:00
For full date time string including timezone offset.
对于包括时区偏移的完整日期时间字符串。
回答by Zaheer Babar
you can render in following way
您可以按以下方式渲染
{{ post.published_at|date("m/d/Y") }}
{{ post.published_at|date("m/d/Y") }}
For more details can visit http://twig.sensiolabs.org/doc/filters/date.html
回答by ihsan
There is a symfony2 tool to display date in the current locale:
有一个 symfony2 工具可以在当前语言环境中显示日期:
{{ user.createdAt|localeDate }} to have a medium date and no time, in the current locale
{{ user.createdAt|localeDate('long','medium') }} to have a long date and medium time, in the current locale
回答by Refilon
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 currentdate in twig, you can use the following:
如果您和我一样,希望在 twig 中显示当前日期,您可以使用以下命令:
{{ "now"|date("m/d/Y") }}
{{ "now"|date("m/d/Y") }}
See documentation about this:
请参阅有关此的文档:
回答by Arnold Richmon
To avoid error on null value you can use this code:
为避免出现空值错误,您可以使用以下代码:
{{ game.gameDate ? game.gameDate|date('Y-m-d H:i:s') : '' }}