对于每个循环来比较数组中的日期

时间:2017-06-24 19:58:33

标签: php mysql arrays loops date

我试图检查用户输入的日期(签入和签出日期),所以首先我创建了一个函数来获取客户选择的日期范围之间的每个日期。

function createRange($startDate, $endDate) {
    $tmpDate = new DateTime($startDate);
    $tmpEndDate = new DateTime($endDate);

    $outArray = array();
    do {
        $outArray[] = $tmpDate->format('Y-m-d');
    } while ($tmpDate->modify('+1 day') <= $tmpEndDate);

    return $outArray;
}

接下来,我创建了一个函数,以便从每个日期的数据库中获取可用性:

function rleft($fromdate, $todate){
    //DB Access
    require('/some/where/over/the/rainbow/db.php');
    //url parameters
    require ('/some/where/over/the/rainbow/param.php');
        //Hotel Room Data
            $customroom = mysqli_query($connection, "SELECT * FROM hotel_room_type WHERE room_type_id = '1'");
            $customroomtotal = mysqli_num_rows($customroom);
            $customroomData = mysqli_fetch_array($customroom);
            $roominventory = $customroomData['room_type_inventory'];
            //availiability list
            $customavailable = mysqli_query($conn, "SELECT * FROM booking_check_avilability WHERE room_type_id = '1' AND check_in_date = '$fromdate' AND check_out_date = '$todate'");
            $customavailabletotal = mysqli_num_rows($customavailable);
            mysqli_close($conn);
            if($customavailabletotal > 0) {
                while($customavailableData = mysqli_fetch_array($customavailable))  {         
                        $inventariodisponible = $roominventory - $customavailableData['total_room'] -$customavailableData['blocked_inventory'];
                }
            } mysqli_free_result($customavailable);
            mysqli_close(conn);
            return $inventariodisponible;
}

我的数据库每个日期有2个日期范围:例如,要知道2017/07/01的可用性,数据库在2列中有此值:

check_in_date | check_out_date
2017/07/01    | 2017/07/02

考虑到这一点

我做了另一个函数,它获取了所选日期之间所有日期的数组,并询问我的rleft函数中每个日期的可用性。

function inout($fromdate, $todate){
 require ('/some/where/over/the/rainbow/param.php');   
    $datearray = createRange("$fromdate", "$todate");
    foreach ($datearray as $value) {
        $from = $value;
        $to = date('Y-m-d',strtotime($value . "+1 days"));
        $left = rleft($from, $to);
    }
    echo $left;
    }

//在param.php中我们有:

function prgrp(){
    $pgrp = "preg_replace('/[^-]a-zA-Z0-9_]/', '', ";
}
//Parametros URL de fechas
    $datefrom = prgrp() . $_GET['checkin'];
    $dateto = prgrp() . $_GET['checkout'];
    $fromdate = date("Y-m-d", strtotime($datefrom));
    $todate = date("Y-m-d", strtotime($dateto));
    $checkin = new DateTime($fromdate);
    $checkout = new DateTime($todate);
    $countdays = $checkin->diff($checkout);
    $cntd = $countdays->format('%d');
    $add1day = date('Y-m-d',strtotime($datefrom . "+1 days"));

如果我回复$ left;我应该得到数组的每个日期有多少房间可用的值...我希望在列表中显示。向客户显示所选日期之间的日期不可用。但由于某些原因,我根本得不到任何东西。

我非常喜欢使用php和编码,所以如果我犯了一些愚蠢的错误,请原谅我,并提前感谢您的帮助。

0 个答案:

没有答案