How to get the last record of the previous month from db in yii2?

I have a table attendacein which there is a different date. Now I want to get traffic for the last record of the previous month. I used this:

$attt = Attendance::find()
    ->select('daytime')
    ->orderBy(['daytime' => SORT_DESC])
    ->one();

to get the last record of the previous month, but she does not give me the last record of the previous month. My tableenter image description here

+4
source share
1 answer

Try to install

$yourMonth = date('m') -1;
$attt = Attendance::find()->select('daytime')
->where(" MONTH( my_date_field) = $yourMonth ")
->orderBy(['daytime'=>SORT_DESC])->one();
+2
source

Source: https://habr.com/ru/post/1608559/


All Articles