How to format a getUpdatedAt() kind of date in Symfony?
date, php, symfony-1.4, symfony1
Solution
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.
Problem
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?