php – How to fetch already existing records in MySql with start_time and end_time in yii2?-ThrowExceptions

Exception or error:

I am having tbl_batch which has

id
title
start_date
start_time
end_time
venue_id
created_by

In the database start_time and end_time is being saved as time()

Row:-

id 1
title test
start_time 10:10
end_time 11:30
venue_id 1
start_date 2020-03-01 (Y-m-d)
created_by 1

So I would like to check if time is between start_time and end_time with the given start_date .

Query I have tried :-

$model = Batch::find()->where([

'start_date' => '2020-03-01',
'venue_id' => 1

])->andWhere([

'AND',
[ '>=',
  'start_time',
  '10:20'
],
[ '<=',
  'end_time',
  '19:20'
]

]);

if($model->exists()){
      echo 'Already exists in DB';
}else
{
      echo 'Does not Exist';
}

This is not returning the correct output .

How to solve:

I finally figured out 🙂


   $time_between = Batch::find()->where([
                   'start_date' => '2020-03-01',
                   'venue_id' => 1
                ])->andWhere([

                    'OR',
                    [
                        'OR',

                        [
                            'between',
                            'end_time',
                            $model->start_time,
                            $model->end_time
                        ],
                        [
                            'between',
                            'start_time',
                            $model->start_time,
                            $model->end_time
                        ]
                    ],
                    [

                        'AND',
                        [
                            '<=',
                            'start_time',
                            $model->start_time
                        ],
                        [
                            '>=',
                            'end_time',
                            $model->start_time
                        ]
                    ]
                ]);

Leave a Reply

Your email address will not be published. Required fields are marked *