简体   繁体   中英

Filter Date using Date Picker in Yii

I have generated my crud screens using gii.. I have a search form, where i have put a date picker, where i give the user to select the date he wants.

But the problem is that i have the date stored in seconds in the database.

and i know that i can convert the date using strtotime. But then how do i filter using the search method in my model?

this is my date picker

<?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;'
        ),
        ));
    ?>

and this is my search method in my model. I want to compare the 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,
    ));
}

Try this:

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

And search

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

Either do a $this->ordering_date = strtotime($this->ordering_date); before you compare or $criteria->compare('ordering_date', strtotime($this->ordering_date));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM