views:

625

answers:

4

I'd like to change the formatting of a date in Symfony 1.4

The default one being:

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

I'd like my date to be formatted like so: 26/01/2010 - 16h23

I tried using the format_date helper DateHelper class.

Unfortunately the API is rather empty (something really needs to be done about it.)

Browsing the helper's source code, I found that a second argument, format, can be passed.

I assumed it was using the same syntax as PHP's date function. But here's what it outputs (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'm sure I'm not the first one having trouble doing this but I've been Googling around and nothing accurate showed up.

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

A: 

How bout going with the default PHP date function? date('d/m/Y', strtotime($question->getUpdatedAt())

robertbasic
I'd prefer using the format_date helper, do you know what keywords the format parameter is using?
Guillaume Flandre
+5  A: 

Have a look at the new functions in 1.4.

You can do:

$question->getDateTimeObject('updated_at')->format('d.m.Y');
// I assume the field's name is 'updated_at'

From the docs:

Date Setters and Getters

We've added two new methods for retrieving Doctrine date or timestamp values as PHP DateTime object instances.

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

You can also set a dates 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 only to work for Doctrine.

Felix Kling
A: 

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

Cristian Navalici
A: 

do you try : echo $question->getUpdatedAt('d/m/y - H\hi') ?

I think it's the easiest way

jaycreation