1

I'm using Kartiks DateRangePicker with the `presetDropdown' option. This gives some default values to search on, today, yesterday, last 7 days etc.

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:

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

So it is querying for items that are created bang on 12am. Does anyone have any ideas what it could be, pretty sure its not the default behavior so must be something I'm doing wrong.

Kyle
  • 667
  • 6
  • 28

2 Answers2

1

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:

enter image description here

Kyle
  • 667
  • 6
  • 28
0

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).

mschristo
  • 1
  • 1