yii2时间范围查询

版权声明:本文由版主林子懿亲自编写,请勿转载!php讨论群-511807472 https://blog.csdn.net/ldTrueLove/article/details/53944518
1.时间范围这个应该都不陌生
但是你要注意你的模型里面只有你数据库的规定字段,现在你需要的是通过两个值即两个字段去查询这一个值所在的范围
2.那么就需要你在模型中定义两个变量
声明并且规定规则
3.然后在试图中进行匹配

模型:
public $start_date;
public $end_date;
public function rules(){
[['start_date','end_date'],'string']
}
//查找方法的时候通过andFilterWhere进行过滤
$this
-> start_date
&& $query ->andFilterWhere([ '>=' , 'date' , new Expression( "to_date(' { $this -> start_date } ', 'yyyy-mm-dd')" )]);
if ( $this -> end_ date) {
$query ->andFilterWhere([ '<' , 'date' , new Expression( "to_date(' { $this -> end_date } ', 'yyyy-mm-dd')" )]);
}
试图:
$form->field($model, 'start_date')->widget(
    DatePicker::className(), [
    'template' => '{addon}{input}',
    'language' => 'zh-CN',
    'clientOptions' => [
        'autoclose' => true,
        'todayHighlight'=>true,
        'format' => 'yyyy-mm-dd'
    ]
]);

$form->field($model, 'end_date')->widget(
    DatePicker::className(), [
    'template' => '{addon}{input}',
    'language' => 'zh-CN',
    'clientOptions' => [
        'autoclose' => true,
        'todayHighlight'=>true,
        'format' => 'yyyy-mm-dd'
    ]
]);

猜你喜欢

转载自blog.csdn.net/ldTrueLove/article/details/53944518