2013-02-19 65 views
2

我使用codeigniter为客户建立租赁网站。我试图只在没有租期符合我的查询时才返回true。基本上有一个开始日期和结束日期的租赁表。Codeigniter:如果日期不在2个值之间,则返回true

我的客户从日期选择器中选择一个开始日期,结束日期是开始日期之后的一组天数。

所以我想知道在这些日期是否有任何出租物品正在出租以验证客户是否可以预订物品。这里是我到目前为止,我需要用Codeigniters活动记录来完成这个...

function check_product($periodLength) { 

    //This is the start time they chose from the picker 
    $startTime = $_POST['date']; 
    //The period length is a variable of days, so the end date would be their chosen start date plus a certain amount of days... 
    $endTime = strtotime('+'.$periodLength.'day', $startTime); 

    $this->db->where('productsId', $_POST['productId']); 
      //This is where I need help!!! What query would give me records that are NOT between the start and end dates that i'm wanting 
    $query = $this->db->get('rentals'); 
    $data = array(); 

    foreach ($query->result() as $row) { 
    $data[] = array(
     'id' => $row->id, 
     'customerId' => $row->customerId, 
     'productsId' => $row->productsId, 
     'periodStart' => $row->periodStart, 
     'periodEnd' => $row->periodEnd, 
     'status' => $row->status, 
     'specialInstructions' => $row->specialInstructions, 
     'adminNotes' => $row->adminNotes 
    ); 
    } 
    return $data; 
} 

我的大部分问题,只是在我的头,我敢肯定,但我需要,如果我的STARTDATE来弄清楚结束日期已被保留。

回答

2

试试这个:

$startTime = $_POST['date']; 
    $endTime = strtotime('+'.$periodLength.'day', $startTime); 

    $this->db->where('productsId', $_POST['productId']); 
    $this->db->where(" $startTime NOT BETWEEN periodStart AND periodEnd AND $endTime NOT BETWEEN periodStart AND periodEnd OR ($startTime < periodStart AND $endTime > periodEnd) "); 
    //Since this is a complex where clause i would recommend 
    //to put enitre clause in single where statement rather than 
    //putting in different where statement. 
    //And it is upto codeigniter active record standards 

    $query = $this->db->get('rentals'); 
相关问题