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
set variable to the current time and date symfony2
提问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 date
function?
您是否尝试过内置date
功能?
$time = date('H:i:s \O\n d/m/Y');
This should work until 2038 :) Both O
and n
need to be escaped, as they have a special meaning within the format string.
这应该工作,直到2038 :)双方O
并n
需要进行转义,因为他们有格式字符串中的特殊含义。
回答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") }}
回答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 O
and n
are necessary to prevent DateTime::format
from interpreting the characters as date codes and output them literally.
的\
前O
和n
是必要的,以防止DateTime::format
从解释字符作为字面日期代码,并将它们输出。