A
A
AlexAll2021-05-01 20:47:28
Yii
AlexAll, 2021-05-01 20:47:28

How to format date in Unix in ModelSearch yii2?

I am using kartik's DatePicker widget to search the gridView through the standard ModelSearch that gii creates, the widget creates a get request like

http://test.local/news?NewSearch=&NewSearch[createDate]=2021-05-20


in controller
public function actionIndex()
    {
        $searchModel = new NewsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


How to do strtotime() of this date and at what location?

I tried to do it in NewsSearch itself, it does not help

$query->andFilterWhere([
            'id' => $this->id,
            'createDate' => strtotime($this->createDate),
        ]);


I thought to do it through jquery when changing, but I think that this is a crutch, how can I do it better?

Widget code is here

<?php
            echo '<label class="control-label">Дата создания</label>';
            echo DatePicker::widget([
            'model' => $model,
            'attribute' => 'createDate',
            'attribute2' => 'createDate',
            'options' => ['placeholder' => 'От'],
            'options2' => ['placeholder' => 'До'],
            'type' => DatePicker::TYPE_RANGE,
            'pluginOptions' => [
                'format' => 'yyyy-mm-dd',
                'autoclose' => true,
            ]
        ]);
    ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question