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

时间:2012-06-06 06:37:01

标签: php 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,
    ));
}

2 个答案:

答案 0 :(得分:1)

试试这个:

$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);

答案 1 :(得分:0)

在比较之前执行$this->ordering_date = strtotime($this->ordering_date);$criteria->compare('ordering_date', strtotime($this->ordering_date));

相关问题