如何在codeigniter中检查行程开始日期和结束日期以及开始时间和结束时间

时间:2018-03-01 10:43:31

标签: php mysql sql codeigniter

我正在使用预订系统,用户可以预订他们的汽车,在数据库/表格中添加任何行程之前我想检查日期和时间之间已存在的行程。 这是我在模型中的查询。

$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();

我已经将值存储在我的旅行表中

tripStart = '2018-03-01'
tripEnd = '2018-03-03'
tripStartTime = '09:00:00'
tripStartEnd = '13:00:00'

2 个答案:

答案 0 :(得分:1)

select trId from trips 
where carId = value AND 
      (Between tripStart = "value" AND tripEnd = "value") AND
      (Between tripsStartTime = "value" AND tripStartEnd = "value");

这就是你想要的东西

答案 1 :(得分:0)

是的,你可以但在此之前你必须连接sql列进行比较
了解SQL CONCAT

此外,您需要使用strtotime PHP函数

将变量用于日期时间
<?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();
?>