比较两个时间戳并根据结果设置语句


Compare 2 timestamps and set a statement based on the result

我有以下表格:

-------------------------------------------
|id|list_id|start_date|end_date|min_nights|
-------------------------------------------
|17|   55  |1437487200|1437735600|3|
|18|   55  |1438005600|1438167600|2|

我想显示每个范围的最小夜数。PHP:

//Seasonal Price
    //1. Store all the dates between checkin and checkout in an array
    $checkin_time = get_gmt_time(strtotime($checkin));
    $checkout_time = get_gmt_time(strtotime($checkout));
    $travel_dates = array();
    $seasonal_prices = array();
    $total_nights = 1;
    $total_price = 0;
    $is_seasonal = 0;
    $i = $checkin_time;
    while ($i < $checkout_time) {
        $i = get_gmt_time(strtotime('+1 day', $i));
        $checkin_date = date('m/d/Y', $i);
        $checkin_date = explode('/', $checkin_date);
        $travel_dates[$total_nights] = $checkin_date[1] . $checkin_date[0] . $checkin_date[2];
        $total_nights++;
    }

    for ($i = 1; $i < $total_nights; $i++) {
        $seasonal_prices[$travel_dates[$i]] = "";
    }
    //Store seasonal price of a list in an array
    $seasonal_query = $this->Common_model->getTableData('seasonalprice', array('list_id' => $id));
    //vaild array checked ilan
    $seasonal_result = $seasonal_query->result_array();
    if ($seasonal_query->num_rows() > 0) {
        foreach ($seasonal_result as $time) {
            //Get Seasonal price
            $seasonalprice_query = $this->Common_model->getTableData(
        'seasonalprice', array(
            'list_id' => $id,
            'start_date' => $time['start_date'],
            'end_date' => $time['end_date']
            ));
            $seasonalprice = $seasonalprice_query->row()->price;
            $seasonal_min_nights = $seasonalprice_query->row()->min_nights;

            //Days between start date and end date -> seasonal price
            $start_time = $time['start_date'];
            $end_time = $time['end_date'];
            $i = $start_time;
            while ($i <= $end_time) {
                $start_date = date('m/d/Y', $i);
                $s_date = explode('/', $start_date);
                $s_date = $s_date[1] . $s_date[0] . $s_date[2];
                $seasonal_prices[$s_date] = $seasonalprice;
                $i = get_gmt_time(strtotime('+1 day', $i));
            }
        }

则有以下条件:

 //half-day booking functionality
    //add time up to 2PM
    $checkin_timestamp = get_gmt_time(strtotime($checkin)) + (14 * 60 * 60);
    //add time up to 11AM
    $checkout_timestamp = get_gmt_time(strtotime($checkout)) + (11 * 60 * 60);

    $query = $this->db->query('SELECT id, list_id FROM `calendar` WHERE `list_id` = "' . $id . '" AND (`booked_days` BETWEEN ' . $checkin_timestamp . ' AND ' . $checkout_timestamp . ') GROUP BY `list_id`');
    $rows = $query->num_rows();        
    $daysexist = $this->db->query("SELECT id,list_id,booked_days FROM `calendar` WHERE `list_id` = '" . $id . "' AND (`booked_days` BETWEEN " . $checkin_timestamp . " AND " . $checkout_timestamp . ") GROUP BY `list_id`");
    $rowsexist = $daysexist->num_rows();
    if ($rowsexist > 0) {
    echo '{"available":false,"total_price":' . $data['price'] . ',"reason_message":"Those dates are not available"}';
    }
    if ($data['guests'] > $capacity) {
        echo '{"available":false,"total_price":' . $data['price'] . ',"reason_message":"' . $capacity . ' guest(s) only allowed"}';
    }
    elseif ($is_seasonal == 1 && $total_nights -1 < $seasonal_min_nights) {
      echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_nights_seasonal'] . ' nights for the following period: '.date('m/d/Y', $start_time).' - '.date('m/d/Y', $end_time).'"}';
        exit;
    }

    $this->session->set_userdata("total_price_'" . $id . "'_'" . $this->dx_auth->get_user_id() . "'", $data['price']);
    $staggered_price = "";
    if ($days >= 30) {
    $staggered_price = ',"staggered_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']) . '","staggered":false';
    }
    elseif (isset($extra_guest)) {
    if ($extra_guest == 1) {
    echo '{"service_fee":"' . get_currency_symbol($id) . get_currency_value_lys($row->currency, get_currency_code(), $data['commission']) . '","extra_guest_price":"' . get_currency_symbol($id) . get_currency_value1($id, $extra_guest_price) . '","extra_guest":1,"reason_message":"","price_per_night":"' . get_currency_symbol($id) . get_currency_value1($id, $per_night) . '","nights":' . $days . ',"available":true,"can_instant_book":false,"total_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']) . '"' . $staggered_price . '}';
    }
    }
    else {
    echo '{"service_fee":"' . get_currency_symbol($id) . get_currency_value_lys($row->currency, get_currency_code(), $data['commission']) . '","reason_message":"","price_per_night":"' . get_currency_symbol($id) . get_currency_value1($id, $per_night) . '","nights":' . $days . ',"available":true,"can_instant_book":false,"total_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']).'"}';
    }

和设置的条件:

        elseif ($is_seasonal == 1 && $total_nights -1 < $seasonal_min_nights) {
      echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_nights_seasonal'] . ' nights for the following period: '.date('m/d/Y', $start_time).' - '.date('m/d/Y', $end_time).'"}';
        exit;
    }

它的工作很好,只要我只有1个自定义范围,在数据库中有2个条目。但是,我所设置的条件总是显示添加到数据库的最后信息,而不是应用于特定时间戳范围的信息。

我如何修改条件以满足时间戳并检索min_nights foreach时间戳范围?谢谢!

我自己解决了这个问题,循环了时间戳,并在循环中设置了我需要使用的变量,然后将条件分别应用于时间戳的每个范围。如果有人有类似的情况:

获取min_nights

$seasonal_min_nights = $seasonalprice_query->row()->min_nights;

while循环:

            $start_time = $time['start_date'];
            $end_time = $time['end_date'];
            $i = $start_time;
            while ($i <= $end_time) {
                $start_date = date('m/d/Y', $i);
                $s_date = explode('/', $start_date);
                $s_date = $s_date[1] . $s_date[0] . $s_date[2];
                $seasonal_nights[$s_date] = $seasonal_min_nights;
                $i = get_gmt_time(strtotime('+1 day', $i));
            }

And for循环:

            //Total Price
            for ($i = 1; $i < $total_nights; $i++) {
            $min_seasonal_nights = $seasonal_nights[$travel_dates[$i]];
            $is_seasonal = 1;
      }

最后一个语句:

     if ($is_seasonal == 1 && $total_nights - 1 < $min_seasonal_nights) {
        echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_seasonal_nights'] . ' nights for the following period: ' . date('m/d/Y', $data['s_start_date']) . ' - ' . date('m/d/Y', $data['s_end_date']) . '"}';
        exit;
    }

我会为此做其他类型的编码,但必须与现有的…希望对大家有所帮助:)