How to format getUpdatedAt () date in symfony?

I want to change date formatting in Symfony 1.4

The default is:

<?php echo $question->getUpdatedAt(); // Returns 2010-01-26 16:23:53 ?> 

I want my date to be formatted like this: 26/01/2010 - 16h23

I tried using the format_date helper class DateHelper .

Unfortunately, the API is pretty empty (something really needs to be done about this.)

Looking through the source code of the helper, I found that the second argument, the format, could be passed.

I assumed that it uses the same syntax as the PHP date function . But here is what it outputs (the same example as above):

 <?php sfContext::getInstance()->getConfiguration()->loadHelpers('Date'); // [...] echo format_date($question->getUpdatedAt(),'d/m/y - H\hi') // Returns 26/23/2010 - 16\4i 

I am sure that I am not the first to have problems with this, but I was googling around and nothing exactly appeared.

Do you have any idea how to format the date in Symfony 1.4?

+6
date php symfony1
source share
4 answers

Check out the new features in version 1.4 .

You can do:

 $question->getDateTimeObject('updated_at')->format('dmY'); // I assume the field name is 'updated_at' 

From the docs:

Date Sensors and Recipients

We have added two new methods for getting Doctrine date and time values ​​as instances of the PHP DateTime object.

 echo $article->getDateTimeObject('created_at')->format('m/d/Y'); 

You can also set the date value by simply calling the setDateTimeObject method and passing a valid DateTime instance.

 $article->setDateTimeObject('created_at', new DateTime('09/01/1985')); 

But it seems that this only works for the Doctrine.

+14
source share

What to do with the default PHP date function? date('d/m/Y', strtotime($question->getUpdatedAt())

+1
source share

You can also use the sfDateFormat class to work with dates. link text

0
source share

You are trying:

 echo $question->getUpdatedAt('d/m/y - H\hi') 

I think this is the easiest way

-4
source share

All Articles