S
S
SlimSavernake2015-11-26 17:02:18
Yii
SlimSavernake, 2015-11-26 17:02:18

How to group query result by date (timestamp) in yii2?

Article table example:
id
title
text
sort_date Query
example:

Article::find()->orderBy('sort_date DESC')->limit(10)->All(),

sort_date - timestamp
As a result, you need to get something like:
10/21/2015
Heading 1
Heading 2
10/20/2015
Heading 3
Heading 4
Heading 5
Heading 6

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Z Sh, 2015-11-26
@SlimSavernake

$articles=Article::find()->orderBy('sort_date DESC')->limit(10)->All();
$sorted_articles=[];
foreach($articles as $article){
      $dt=date('d.m.Y',$article->sort_date);
      $sorted_articles[$dt][]=$article;
}

M
matperez, 2015-11-26
@matperez

Try something like

->select('article.*, DATE_FORMAT(FROM_UNIXTIME(`article.sort_date`), \'%d.%m.%Y\') as date')
->orderBy('sort_date')

Well, the public property date can be added to the model so that there is a place to save the value itself.
And you can scatter into an array for specific dates already at the PHP level.

L
LittleFatNinja, 2015-11-26
@LittleFatNinja

grouped dy sort_date

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question