如何在Symfony中格式化一个getUpdatedAt()类型的日期? [英] How to format a getUpdatedAt() kind of date in Symfony?

查看:112
本文介绍了如何在Symfony中格式化一个getUpdatedAt()类型的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Symfony 1.4中更改日期的格式

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

默认值为:

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

我希望我的日期格式如下: 26/01 / 2010 - 16h23

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

我尝试使用 format_date helper DateHelper class。

I tried using the format_date helper DateHelper class.

不幸的是, API 是相当空的(真的需要做的事情)。

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.

我认为它使用的语法与 PHP的日期功能
但是这是它的输出(与上述相同的例子):

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

我相信我不是第一个有麻烦的人这个,但是我一直在Googling,没有准确的显示。

I'm sure I'm not the first one having trouble doing this but I've been Googling around and nothing accurate showed up.

你有什么想法如何在Symfony 1.4格式化日期?

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

推荐答案

看看 1.4中的新功能

您可以:

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

从文档中: p>

From the docs:


日期Setters和Getters

我们添加了两种用于将 Doctrine 日期或时间戳值作为PHP DateTime对象实例检索的新方法。

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');

您还可以通过调用setDateTimeObject方法并传递有效的DateTime实例来设置日期值。 / p>

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'));


但它似乎只适用于Doctrine。

But it seems only to work for Doctrine.

这篇关于如何在Symfony中格式化一个getUpdatedAt()类型的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆