S
S
Silverviql2018-04-24 15:30:37
Yii
Silverviql, 2018-04-24 15:30:37

How to remove time from a date when getting data from a database model?

I tried to write in the query ->select('DATE_FORMAT(date, "%m-%Y") as data')
Gives an error SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `zakaz` WHERE (action <= 0) AND (data >= (CURDATE()-1) AND data < CURDATE()' at line 1
I need it to group the date.Now everything looks like this for me.It turns out to combine only if the time also matches.
5adf23083cf40372464096.png

$Articles = new ActiveDataProvider([
    'query' => \app\models\Zakaz::find()
        ->where('action <= 0') ->andWhere('data >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)'),
    'pagination' => [
        'pageSize' => 20,
    ],
]);

I display a table
<?php echo GridView::widget([
        'dataProvider' => $Articles,
        'showFooter' => true,
        'filterModel'=>$searchModel,
        /*'showPageSummary'=>true,*/
        'pjax'=>true,
        'striped'=>true,
        'hover'=>true,
        'panel'=>['type'=>'primary', 'heading'=>'За неделю'],
        'columns' => [
            [
                'attribute'=>'data',
                'width'=>'310px',
                'value'=>function ($model, $key, $index, $widget) {
                    return $model->data;
                },
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>ArrayHelper::map(\app\models\Zakaz::find()->orderBy('data')->asArray()->all(), 'id_zakaz', 'data'),
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>'Any supplier'],
                'group'=>true, // enable grouping
            ],

            [
                'class' => NumberColumn::className(),
                'attribute' => 'fact_oplata',
            ],
        ],
    ]);;?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2018-04-24
@Silverviql

removing the time from the date is not at the level of the SQL query, but at the level of data output in the GridView

...
'columns' => [
    [
        'attribute' => 'date',
        'format' => ['date', 'php:d-m'-y]
]
...

B
Boris Korobkov, 2018-04-24
@BorisKorobkov

->select('DATE_FORMAT(date, "%m-%Y") as data')

PS "data" - this is data, not a date at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question