简体   繁体   English

Yii2 Kartik DateRangePicker。

[英]Yii2 Kartik DateRangePicker.

I'm using Kartiks DateRangePicker with the `presetDropdown' option.我使用Kartiks DateRangePicker与`presetDropdown”选项。 This gives some default values to search on, today, yesterday, last 7 days etc.这提供了一些默认值来搜索,今天,昨天,过去 7 天等。

The code to produce the picker:生成选择器的代码:

echo DateRangePicker::widget([
    'model'=>$model,
    'attribute' => 'created_at',
    'useWithAddon'=>true,
    'convertFormat'=>true,
    'presetDropdown'=>true,
    'hideInput'=>true,
    'startAttribute' => 'start',
    'endAttribute' => 'end',
    'pluginOptions'=>[
        'locale'=>['format' => 'Y-m-d'],
    ]
]);

SearchItem class:搜索项类:

  $this->start = strtotime($this->start);
  $this->end = strtotime($this->end);

  $query->andFilterWhere(['>=', 'created_at', $this->start])
        ->andFilterWhere(['<', 'created_at', $this->end]);

The problem I'm having is when using today or yesterday options it produces the following query:我遇到的问题是,在使用todayyesterday选项时,它会产生以下查询:

SELECT * FROM `item` WHERE (`created_at` >= 1534896000) AND (`created_at` < 1534896000)

So it is querying for items that are created bang on 12am.所以它正在查询在 12 点创建的项目。 Does anyone have any ideas what it could be, pretty sure its not the default behavior so must be something I'm doing wrong.有没有人知道它可能是什么,很确定它不是默认行为,所以一定是我做错了。

My bad...我的错...

Solved with by editing the format option to:通过将格式选项编辑为:

echo DateRangePicker::widget([
    'model'=>$model,
    'attribute' => 'created_at',
    'useWithAddon'=>true,
    'convertFormat'=>true,
    'presetDropdown'=>true,
    'hideInput'=>true,
    'startAttribute' => 'start',
    'endAttribute' => 'end',
    'pluginOptions'=>[
        'locale'=>['format' => 'Y-m-d H:i:s'],
    ]
]);

Produces an uglier looking input field but there will be a way to hide that:产生一个看起来更丑的输入字段,但有一种方法可以隐藏它:

在此处输入图片说明

Alternatively, you can use with your first version something like this to get the proper end timestamp :或者,您可以在第一个版本中使用类似这样的方法来获得正确的结束时间戳:

strtotime('+1 day', $this->end) - 1

With the above command, you get the timestamp of the beginning of the next day (ex. 14/11 00:00:00) and you subtract one second from it, to get the timestamp you want ( in the same example 13/11 23:59:59).使用上面的命令,您可以获得第二天开始的时间戳(例如 14/11 00:00:00),然后从中减去一秒,以获得您想要的时间戳(在同一个示例中 13/11 23:59:59)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM