日期字段搜索不过滤'dd / mm / yyyy'格式 - YII框架

时间:2013-12-25 06:33:06

标签: yii

CJUIDatePicker没有过滤dd / mm / yyyy格式的值,但如果我手动提供DB日期格式,如图所示,我已经附加了所有代码,包括查看和模型搜索面板和日期窗口小部件,请查看此内容,< / p>

我的模特搜索(),

public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('crm_base_contact_id',$this->crm_base_contact_id);
        $criteria->compare('name',$this->name,true);
        $Date = date('Y-m-d',strtotime($this->created)); // get proper Y-m-d
        $startOfDay = $Date . ' 00:00:00'; // from start of the day
        $endOfDay = $Date . ' 23:59:59';   // until end of the day

// the rows between these
        $criteria->addBetweenCondition('created', strtotime($startOfDay) , strtotime($endOfDay) );

        $criteria->compare('createdby',$this->createdby,true);
        $criteria->compare('description',$this->description);
        $criteria->compare('is_active',$this->is_active);
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

我的CGridView视图,

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'basecontact-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        array('class'=>'CButtonColumn',),
        'name',
        'crm_base_contact_id',      
                   array(
            'name'=>'is_active',
            'filter'=>CHtml::dropDownList('Event[is_active]', '',  
                array(
                    ''=>'',
                    '1'=>'Y',
                    '0'=>'N',
                )
            ),
            'value' =>'($data->is_active==1)?"Y":"N"',
        ),
        'created',
        'createdby',
        'description',
    ),
)); ?>

我的搜索视图代码列,

<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
            'model'=>$model,
             'id'=>'Search-Created',
            'attribute'=>'created',
                        'options'=>array(
                     'dateFormat'=>'dd/mm/yy',
                     'showAnim'=>'fold',
                      'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
                      'buttonImageOnly'=>true,
                      'buttonText'=>'',
                      'showAnim'=>'fold',
                      'showOn'=>'button',
                      'showButtonPanel'=>false,
            ),
));?>

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该更改日期格式。设置如下,'dateFormat'=&gt;'dd-mm-yy',

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'dob',
// additional javascript options for the date picker plugin
'options' => array(
'showAnim' => 'fold',
'dateFormat'=>'dd/mm/yyyy',
),
'htmlOptions' => array(
'style' => 'height:20px;'
),
));

<强>更新

在你的模型中,将其定义如下,在我的例子中,dob是属性名称。你可以拥有自己的。

在您运行任何查找之前,这会将日期格式从“dd / mm / yyyy”更改为“yyyy-mm-dd”。

参考:http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeFind-detail

protected function beforeFind()
{
$this->dob=date('Y-m-d', strtotime($this->dob)); 
parent::beforeFind();
}

更新2

将此搜索方法替换为

public function search() {
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria = new CDbCriteria;

    $criteria->compare('crm_base_contact_id', $this->crm_base_contact_id);
    $criteria->compare('name', $this->name, true);    
    $criteria->compare('created', '07/12/2013'); //date('Y-m-d', strtotime($this->created))
    $criteria->compare('createdby', $this->createdby, true);
    $criteria->compare('description', $this->description);
    $criteria->compare('is_active', $this->is_active);
    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

更新3 将此行$criteria->compare('created', '07/12/2013');更改为

$criteria->compare('created', date('Y-m-d', strtotime($this->created));