How to write where Between request in yii2

I give $start_dateand $end_dateas a parameter for comparison with the field createdin the database table MySQL. I am using the yii2framework.

Here is what I tried:

$modelStockDetails=StockDetails::find()->where(['BETWEEN', 'created', $start_date, $end_date])->andwhere(['receiving_order_id' =>$modelRecevingOrder->id,'deleted' => 'N'])->all();

which returns an empty array when the values $start_dateand $end_datediffer from the date createdin the table.

But it returns an array containing the data when I pass $start_date, which is exactly the same as the createddate in the table.

+1
source share
1 answer

, try str_to_date literal ( , , "% d-% m-% Y" )

$modelStockDetails=StockDetails::find()
  ->where(' date(created) between STR_TO_DATE("'.  $start_date . '", "%d-%m-%Y" ) 
        AND   STR_TO_DATE("' .  $end_date . '", "%d-%m-%Y" )' )
  ->andwhere(['receiving_order_id' =>$modelRecevingOrder->id,'deleted' => 'N'])->all();

var sql

$modelStockDetails=StockDetails::find()
->where(' date(created) between STR_TO_DATE(:start_date, "%d-%m-%Y" ) 
      AND   STR_TO_DATE( :end_date, "%d-%m-%Y" )', [':start_date' => $start_date, ':end_date' => $end_date] )
->andwhere(['receiving_order_id' =>$modelRecevingOrder->id,'deleted' => 'N'])->all();
+1

All Articles