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

问题描述 投票:2回答:3

我正在使用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时间戳?

unix gridview datepicker yii2
3个回答
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数据库列。


0
投票

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

 [
        'attribute'=>'created_date',
            'value' => function ($model, $index, $widget) {
           $date = date('your date format eg.Y-m-d H:m:s', strtotime($model->created_date);
            return 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',
    ],

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保证工作

© www.soinside.com 2019 - 2024. All rights reserved.