【问题标题】:Codeigniter: Return true if dates are NOT between 2 valuesCodeigniter:如果日期不在 2 个值之间,则返回 true
【发布时间】:2013-02-19 22:30:50
【问题描述】:

我正在使用 codeigniter 为客户构建一个租赁网站。仅当没有租赁期与我的查询匹配时,我才尝试返回 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 到 enddate 期间是否已被保留。

【问题讨论】:

    标签: php codeigniter activerecord


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多