Yii 框架日期范围

Yii Framework Date Range

我在 Yii 中找不到日期范围,如果 FROM_DATE: 2015-02-05 和 to_date: 2015-02-10,在 gridview 中只显示带有日期的数据05,06,07,08,09 且仅标注日期为 10-02,但未出现。 在另一种情况下,如果我想搜索这样的日期范围 from_date : 2015-02-21 to_date: 2015-02-21,日期为 2015-02-21 的数据应该出现 如何解决这个问题?它应该如何是正确的代码?

型号

public function search()
{
    $criteria=new CDbCriteria;
if(!empty($this->from_date) && empty($this->to_date)){
        $criteria->condition="create_date >= '$this->from_date'";
    }elseif(!empty($this->to_date) && empty($this->from_date)){
        $criteria->condition="create_date <= '$this->to_date'";
    }elseif(!empty($this->to_date) && !empty($this->from_date)){
        $criteria->condition="create_date >= '$this->from_date' and create_date<= '$this->to_date'";
    }
    $criteria->compare('user_id',$this->user_id);
    $criteria->compare('lokasi',$this->lokasi,true);
    $criteria->compare('satisfy_val',$this->satisfy_val,true);
    $criteria->compare('create_date',$this->create_date,true);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
        'sort'=>array(
        'defaultOrder'=>array('create_date DESC',)),
            'pagination'=>array(
                    'PageSize'=>150
            ),
    ));
}

谢谢你

您可以使用以下代码搜索日期范围

if((isset($this->from_date) && trim($this->from_date) != "") && (isset($this->to_date) && trim($this->to_date) != "")){
        $criteria->condition = ' create_date>="'.date("Y-m-d",strtotime($this->from_date)).'" AND create_date<="'.date("Y-m-d",strtotime($this->to_date)).'"';
}

使用以上代码后,您的代码如下所示:

public function search()
{
    $criteria=new CDbCriteria;
    if((isset($this->from_date) && trim($this->from_date) != "") && (isset($this->to_date) && trim($this->to_date) != "")){
        $criteria->condition = ' create_date>="'.date("Y-m-d",strtotime($this->from_date)).'" AND create_date<="'.date("Y-m-d",strtotime($this->to_date)).'"';
    }
    $criteria->compare('user_id',$this->user_id);
    $criteria->compare('lokasi',$this->lokasi,true);
    $criteria->compare('satisfy_val',$this->satisfy_val,true);
    $criteria->compare('create_date',$this->create_date,true);
    return new CActiveDataProvider($this, array(
       'criteria'=>$criteria,
       'sort'=>array(
       'defaultOrder'=>array('create_date DESC',)),
        'pagination'=>array(
                'PageSize'=>150
        ),
  ));
}