Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set variable to the current time and date symfony2

Tags:

php

symfony

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?

like image 985
user2269869 Avatar asked May 08 '13 13:05

user2269869


3 Answers

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.

like image 67
Maerlyn Avatar answered Nov 07 '22 07:11

Maerlyn


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.

like image 44
likeitlikeit Avatar answered Nov 07 '22 08:11

likeitlikeit


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

like image 24
cheesemacfly Avatar answered Nov 07 '22 07:11

cheesemacfly