如何在Yii2 GridView中过滤unix时间戳?

时间:2015-12-22 10:09:30

标签: unix gridview datepicker yii2

我正在使用Kartik GridView,我的日期列设置如下

    [
        'attribute'=>'created_date',
            'value' => function ($model, $index, $widget) {
            return Yii::$app->formatter->asDate($model->created_date);
        },
        'format' => ['date', 'php:d.m.Y'],
        'filterType' => GridView::FILTER_DATE,
        'filterWidgetOptions' => [
        'pluginOptions' => [
            'format' => 'yyyy-mm-dd',
            'autoclose' => true,
            'todayHighlight' => true,
        ]
        ],
        'width' => '160px',
        'hAlign' => 'center',
    ],

搜索模型

[['created_date'], 'safe'],

$query->andFilterWhere([
            'links_id' => $this->links_id,
            'modified_date' => $this->modified_date,
            'created_date' => $this->created_date,
        ]);

created_date 是创建记录时自动生成的数据库中的列,其格式如下 2015-12-16 13:42:09.425618

虽然日期选择器工作但我得到SQL错误:

The SQL being executed was: SELECT COUNT(*) FROM "links" LEFT JOIN "countries" ON "links"."country" = "countries"."country" WHERE "created_date"='2015-12-15'

那么,如何使用Yii2 kartik日期选择器正确过滤unix时间戳?

3 个答案:

答案 0 :(得分:1)

好的,谢谢@ck_arjun。我想通了。所以在搜索模型中代码看起来应该是这样的

// Convert date to Unix timestamp
if (!empty($params['LinksSearch']['created_date'])) {
      $query->andFilterWhere([
        '(links.created_date::DATE)' => date('Y-m-d',strtotime($params['LinksSearch']['created_date']))
      ]);
 }

这样您就可以用自己的格式传递日期来过滤unix数据库列。

答案 1 :(得分:0)

在网格视图中尝试此操作:

>>> from sympy import *
>>> s=FiniteSet(range(0,3))
>>> x=symbols("x")
>>> x in s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\lhk\Anaconda3\lib\site-packages\sympy\sets\sets.py", line 497, in __contains__
    raise TypeError('contains did not evaluate to a bool: %r' % symb)
TypeError: contains did not evaluate to a bool: Contains(x, {range(0, 3)})
>>> x=3
>>> x in s
False
>>> Contains(x,s)
False
>>>

答案 2 :(得分:0)

在返回$ dataProvider

之前的搜索模型中

插入下一个代码:

    if($this->created_normal)
    $query->andFilterWhere(['between', 'created_at', strtotime($this->created_normal), strtotime($this->created_normal)+86400]);
if($this->updated_normal)
    $query->andFilterWhere(['between', 'updated_at', strtotime($this->updated_normal), strtotime($this->updated_normal)+86400]);

updated_normal - 搜索模型中的帮助字段(yyyy-mm-dd)

对int

保证工作