How to format a getUpdatedAt() kind of date in Symfony?

隐身守侯 提交于 2019-12-21 04:54:10

问题


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?


回答1:


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.




回答2:


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




回答3:


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




回答4:


do you try :

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

I think it's the easiest way



来源:https://stackoverflow.com/questions/2141252/how-to-format-a-getupdatedat-kind-of-date-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!