I am working on Booking system where a user can book their cars, before adding any trip inside the database/table I want to check the trip already exist between the dates and times. Here is my query inside my model.
$this->db->select('trId')
->from('trips')
->where('carId',$data['carId'])//car Id
->where('tripStart <=',$data['tripStart'])//trip start date
->where('tripEnd >= ',$data['tripEnd'])//trip end date
->where('tripStartTime <= ',$data['tripStartTime'])//trip start time
->where('tripStartEnd >=',$data['tripStartEnd'])//trip end time
->get();
I have stored the values in my trips table is
tripStart = '2018-03-01'
tripEnd = '2018-03-03'
tripStartTime = '09:00:00'
tripStartEnd = '13:00:00'
select trId from trips
where carId = value AND
(Between tripStart = "value" AND tripEnd = "value") AND
(Between tripsStartTime = "value" AND tripStartEnd = "value");
This that What you want
Yes you can but before that you have to concate sql columns for comparing
Know SQL
CONCAT
Also you need to combine your variables using into datetime using strtotime
PHP function
<?php
$tripStart = '2018-03-01';
$tripEnd = '2018-03-03';
$tripStartTime = '09:00:00';
$tripStartEnd = '13:00:00';
$from = date('Y-m-d H:i:s',strtotime($tripStart.$tripStartTime));
$to = date('Y-m-d H:i:s',strtotime($tripEnd.$tripStartEnd));
$this->db->select('trId')
->from('trips')
->where('carId',1)//car Id
->where("CONCAT(tripStart,'',tripStartTime) > ",$from)
->where("CONCAT(tripEnd,'',tripStartEnd) < ",$to)
->get();
?>