6

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?

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.