I want to set a variable $time to the current time and then date following this format: HH:mm:ss On day/month/year.
Could anyone help me to do that in symfony?
Have you tried with the built-in date
function?
$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.
This is the same as in any other PHP application:
$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.
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:
{{ "now"|date("H:i:s \O\n d/m/Y") }}
http://twig.sensiolabs.org/doc/filters/date.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With