L
L
lolka022019-02-11 13:43:22
Yii
lolka02, 2019-02-11 13:43:22

How to select records for today, yesterday and so on in yii2?

how to write a condition in the filter so that records are selected for today, yesterday, etc. field in unix database

$tomorrow = strtotime('+1 days');
..
 ->where(['>=', 'created_at', mktime(0,0,0,date('m'),date('d'),date('Y'))])
 ->andWhere(['<', 'created_at', mktime(0,0,0,date('m', $tomorrow),date('d', $tomorrow),date('Y', $tomorrow))])

is that the only way? or are there other ways? simpler

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lusin, 2019-02-11
@lusin

The request itself will be something like this:

SELECT FROM_UNIXTIME(t.created_at), t.*
 from table_name t
 where  (FROM_UNIXTIME(t.created_at) + interval 1 day) >= now()

In Yii, something like this:
$lines = $this->find()
->alias('t')
->where("(FROM_UNIXTIME(t.created_at) + interval :days day) >= now()", [
':days' => $day
])
->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question