Use your time well

Welcome to my blog, I hope you will enjoy and you never deny to come back to visit again.
"USE YOUR FINGER TO CODE AND YOUR BRAIN TO THINK"


Search This Blog

Thursday, July 17, 2014

How to deal with date format in search under YII Framework

There is one problem we are facing about date format. As far as we know, in DBMS there will be the default date format. You can change it from the database itself or you can deal with it from your php codes.

I have a scenario, my date format for the database is '2014-12-12' but if you are dealing with end user, that is not the format which you can go with it. May be you would prefer '12.12.2014' which will be easy to deal with it.

So lets say that you want to search your data according to date and you want to use '12.12.2014' for end user then you can do this:

we know about model and view right in YII.

under view admin you can do this in CgridView

<?php

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'123erer',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(

array('name'=>'p_date',
'value'=>'date("d.m.Y",Strtotime($data->p_date))',
),
array(
'class'=>'CButtonColumn',
'template'=>'{view}',

'buttons'=>array(
           // 'update'=>array(
//                       'visible'=>$update1,
//                            ),

  ),

),
),
)); ?>

and under your model you can do this:

change the function search to this function below

<?php

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

$criteria=new CDbCriteria;

if(!empty($this->p_date))
{
$criteria->compare('p_date',date('Y-m-d',Strtotime($this->p_date)),true);

}
        
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
?>



******Enjoy******



No comments: