【问题标题】:How to filter unix timestamp in Yii2 GridView?如何在 Yii2 GridView 中过滤 unix 时间戳?
【发布时间】:2015-12-22 10:09:30
【问题描述】:

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

【问题讨论】:

  • 错误是什么?
  • @ck_arjun 不明确的列:7 错误:列引用“created_date”不明确
  • 您的链接表和国家/地区表。如果两者都有“created_date”字段,您应该在适用的情况下指定表名和字段。 "links.created_date" => $this->created_date

标签: unix gridview datepicker yii2


【解决方案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 数据库列。

【讨论】:

    【解决方案2】:

    在你的网格视图中试试这个:

     [
            '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',
        ],
    

    【讨论】:

    • 试过但仍然收到此错误The SQL being executed was: SELECT COUNT(*) FROM "links" LEFT JOIN "countries" ON "links"."country" = "countries"."country" WHERE "created_date"='2015-12-15'。也试过这个解决方案stackoverflow.com/questions/28317351/…,但得到The SQL being executed was: SELECT COUNT(*) FROM "links" LEFT JOIN "countries" ON "links"."country" = "countries"."country" WHERE "created_date"=1450137600
    【解决方案3】:

    在返回 $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保证工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 2017-02-12
      相关资源
      最近更新 更多