A
A
andr20172016-09-07 16:57:02
Yii
andr2017, 2016-09-07 16:57:02

How to organize sorting by date in Yii in CActiveDataProvider?

In general, the date is stored in datetime in the database. I output data and change the date format when outputting. How can I sort by this field?
CActiveDataProvider code:

$tripsData = new CActiveDataProvider('Trips',[
                'criteria'=>$criteriaToday,
                'pagination' => [
                    'pageSize' => $this->per_page
                ],
                'sort'=>[
                    'attributes' => [
                        'id',
                        'place',
                        'target',
                        'execution_date',
                        'date',
                    ],
                    'defaultOrder'=>[
                        'id'=>'DESC',
                    ]],
            ]);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
andr2017, 2016-09-09
@andr2017

it was necessary to specify the name of the attribute in column.
[
'name' => 'date',
'type' => 'raw',
'value' => 'strtotime($data->date) ? date(\'dmY H:i\', strtotime($data->date)) : \'\'',
],

B
BATPYIIIKOB, 2016-09-08
@BATPYIIIKOB

And it doesn't work?

'defaultOrder'=>[
    'date'=>'DESC',
 ]],

or change the sequence insort

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question