dpppic5186 2018-08-23 07:48
浏览 295

Yii2 Kartik DateRangePicker。

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.

  • 写回答

2条回答 默认 最新

  • douwa0280 2018-08-23 08:02
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line