我在 CGridView 中将日期显示为:“22.6.2012 22:53”:
array('name' => 'date',
'value' => date("j.n.Y G:i", strtotime($model->date))
),
但在我的过滤器中,我需要以这种格式(在数据库中)搜索以获得结果:“2012-06-22 22:53”。
如何使我的过滤器以 CGridView 中显示的格式工作?我已经搜索了答案但没有找到答案,我还尝试在模型 search() 中为此属性添加日期函数:
$criteria->compare('date', date("j.n.Y G:i", strtotime($this->date), true);
但后来我只得到一个空列表:)
帮助将不胜感激。
首先,您不应该使用value
属性来控制日期的格式。正确的方法是设置type http://www.yiiframework.com/doc/api/1.1/CDataColumn#type-detail财产给'date'
并且,如果您还没有这样做,请设置CApplication.language http://www.yiiframework.com/doc/api/1.1/CApplication#language-detail以适当的区域设置为目标。
对于过滤器,如果您使用CJuiDatePicker
小部件让用户直观地选择日期;有一个关于如何做到这一点的简短而切中要害的指南here http://www.yiiframework.com/wiki/318/using-cjuidatepicker-for-cgridview-filter/.
Update:
格式化列type == 'date'
是通过完成CGridView.formatter http://www.yiiframework.com/doc/api/1.1/CGridView#formatter,如果您没有明确设置一个值,则默认值是'format'
应用程序组件是。因此您可以指定并配置CFormatter http://www.yiiframework.com/doc/api/1.1/CFormatter当场,或者如果您想使用应用程序的格式化程序,但只需稍作修改即可
$formatter = clone Yii::app()->format;
$formatter->dateFormat = 'whatever'; // or $formatter->dateTimeFormat
然后将此实例分配给CGridView.formatter
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)