php 将变量设置为当前时间和日期 symfony2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16442634/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:08:47  来源:igfitidea点击:

set variable to the current time and date symfony2

phpsymfony

提问by user2269869

I want to set a variable $time to the current time and then date following this format: HH:mm:ss On day/month/year.

我想将变量 $time 设置为当前时间,然后按照以下格式设置日期:HH:mm:ss On day/month/year。

Could anyone help me to do that in symfony?

谁能帮我在 symfony 中做到这一点?

回答by Maerlyn

Have you tried with the built-in datefunction?

您是否尝试过内置date功能?

$time = date('H:i:s \O\n d/m/Y');

This should work until 2038 :) Both Oand nneed to be escaped, as they have a special meaning within the format string.

这应该工作,直到2038 :)双方On需要进行转义,因为他们有格式字符串中的特殊含义。

回答by cheesemacfly

As you are using Symfony2 and you are looking for a way to display a date, you can do this directly in your twig template with:

当您使用 Symfony2 并且您正在寻找一种显示日期的方法时,您可以直接在您的树枝模板中执行此操作:

{{ "now"|date("H:i:s \O\n d/m/Y") }}

http://twig.sensiolabs.org/doc/filters/date.html

http://twig.sensiolabs.org/doc/filters/date.html

回答by likeitlikeit

This is the same as in any other PHP application:

这与任何其他 PHP 应用程序中的相同:

$time = new \DateTime();
echo $time->format('H:i:s \O\n Y-m-d');

The \before Oand nare necessary to prevent DateTime::formatfrom interpreting the characters as date codes and output them literally.

\On是必要的,以防止DateTime::format从解释字符作为字面日期代码,并将它们输出。