L
L
Lev K2016-05-18 20:00:26
Yii
Lev K, 2016-05-18 20:00:26

How to organize a query with a date range in yii2?

You need to sample over a period.
I do this but it doesn't work

$leftDate = '2016-01-01'; 
$rightDate = date('Y-m-d');
$trips = Trip::find()
            ->joinWith(['idDrivers'])
            ->where('Date_start >= :left and Date_end <= :right', [':left' => $leftDate, ':right' => $rightDate])
            ->all();

The problem is probably that the dates in the database are stored in the dateTime format, they have time. I only need dates.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-05-19
@Leffken

$leftDate = '2016-01-01'; 
$rightDate = date('Y-m-d');
$trips = Trip::find()
            ->joinWith(['idDrivers'])
            ->where('DATE(Date_start) >= :left and DATE(Date_end) <= :right', [':left' => $leftDate, ':right' => $rightDate])
            ->all();

R
Rikcon, 2016-05-18
@Rikcon

tigor.com.ua/blog/2008/08/23/date_comparison_by_be...
You need BETWEEN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question