CGridView 中的格式化日期过滤器 [英] Formatted date filter in CGridView

查看:25
本文介绍了CGridView 中的格式化日期过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 CGridView 中显示我的日期为:22.6.2012 22:53":

I display my date in CGridView as: "22.6.2012 22:53" with:

array('name' => 'date',
            'value' => date("j.n.Y G:i", strtotime($model->date))
        ),

但是在我的过滤器中,我需要以这种格式(在数据库中)进行搜索才能得到结果:2012-06-22 22:53".

But in my filter, I need to search in this format (which is in the database) to get results: "2012-06-22 22:53".

如何使我的过滤器以我的 CGridView 中显示的格式工作?我已经搜索了一个答案,但没有找到,我也尝试在我的模型 search() 中为此属性添加日期函数:

How can I make my filter to work in the format that is displayed in my CGridView? I've searched for an answer but haven't found one, I've also tried adding the date function in my model search() for this attribute:

$criteria->compare('date', date("j.n.Y G:i", strtotime($this->date), true);

但后来我得到了一个空列表:)

but then I just get an empty list :)

不胜感激.

推荐答案

compare() 用输入生成一个 sql 语句,所以我不得不将输入更改为我想要的格式.

compare() makes a sql sentence with the input, so I had to change the input to my wanted format.

我的功能:

function changeDateToDBformat($datum) {
        if (strstr($datum, '.') || strstr($datum, ':')) {
            $formats = array('!j.n', '!j.n.Y', '!j.n.Y H:i', '!n.Y H:i', '!n.Y', '!H:i', '!j.n.Y H', '!n.Y H', '!Y H:i', '!Y H');
            $date = false;

            foreach ($formats as $format) {
                $date = DateTime::createFromFormat($format, $datum);
                if (!($date === false)) {
                    $izbraniFormat = $format;
                    break;
                }
            }

            if (!$date === false) {
                $datum1 = $date->format('Y-m-d H:i');
                $date2 = DateTime::createFromFormat(substr($izbraniFormat, 1, strlen($izbraniFormat)), $datum);
                $datum2 = $date2->format('Y-m-d H:i');

                $datumcas1 = explode(' ', $datum1);
                $datumcas2 = explode(' ', $datum2);

                $prvidatum = explode('-', $datumcas1[0]);
                $drugidatum = explode('-', $datumcas2[0]);
                $koncniDatum = '';
                for ($a = 0; $a < sizeof($prvidatum); $a++) {
                    if ($prvidatum[$a] == $drugidatum[$a])
                        $koncniDatum .= '-' . $prvidatum[$a];
                }
                $koncniCas = '';
                $prvicas = explode('-', $datumcas1[1]);
                $drugicas = explode('-', $datumcas2[1]);
                for ($a = 0; $a < sizeof($prvicas); $a++) {
                    if ($prvicas[$a] == $drugicas[$a])
                        $koncniCas .= ':' . $prvicas[$a];
                }
                $koncniDatum = substr($koncniDatum, 1, strlen($koncniDatum));
                if (strlen($koncniCas) > 0)
                    $koncniDatum .= ' ' . substr($koncniCas, 1, strlen($koncniCas));

                $datum = $koncniDatum;
            }
        }
        return $datum;
    }
//translations:
//izbrani == selected
//datum == date
//cas == time
//koncni == end
//prvi == first
//drugi == second

有了这个,用户可以以j.n.Y H:i"格式输入日期,也可以输入此格式的一部分(j.n, n.Y, Y H:i,...).

With this, a user can enter date in the format "j.n.Y H:i" and also just portions of this format (j.n, n.Y, Y H:i,...).

我要感谢 Jon 和 nickb 的帮助!链接

I would like to thank Jon and nickb for help! link

这篇关于CGridView 中的格式化日期过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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