使用 Yii 中的日期选择器过滤日期


Filter Date using Date Picker in Yii

我用gii生成了我的crud屏幕。我有一个搜索表单,其中我放置了一个日期选择器,我让用户选择他想要的日期。

但问题是我在数据库中以秒为单位存储了日期。

而且我知道我可以使用 strtotime 转换日期。但是,如何在模型中使用搜索方法进行过滤呢?

这是我的日期选择器

<?php 
        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
        'name'=>'ordering_date',
        'id'=>'ordering_date',
        // additional javascript options for the date picker plugin
        'options'=>array(
            'showAnim'=>'fold',
        ),
        'htmlOptions'=>array(
            'style'=>'height:20px;'
        ),
        ));
    ?>

这是我在模型中的搜索方法。我想比较ordering_date

public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.
    //echo $this->ordering_date;
    $criteria=new CDbCriteria;
    $criteria->compare('order_id',$this->order_id);
    $criteria->compare('customer_id',$this->customer_id);
    $criteria->compare('delivery_address_id',$this->delivery_address_id);
    $criteria->compare('billing_address_id',$this->billing_address_id);
    $criteria->compare('ordering_date',$this->ordering_date);
    $criteria->compare('ordering_done',$this->ordering_done,true);
    $criteria->compare('ordering_confirmed',$this->ordering_confirmed);
    $criteria->compare('payment_method',$this->payment_method);
    $criteria->compare('shipping_method',$this->shipping_method);
    $criteria->compare('comment',$this->comment,true);
    $criteria->compare('status',$this->status,true);
    $criteria->compare('total_price',$this->total_price);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

试试这个:

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
        'model'=>$model,
        'attribute'=>'ordering_date',
        'options'   => array('dateFormat' => 'mm-dd-yy',),

和搜索

$begindate = CDateTimeParser::parse($this->ordering_date, 'MM-dd-yyyy');
$enddate   = $begindate + 24* 60*60;
$criteria->addBetweenCondition('ordering_date', $begindate, $enddate);

在比较之前先做一个$this->ordering_date = strtotime($this->ordering_date);,要么$criteria->compare('ordering_date', strtotime($this->ordering_date));