在PHP和Mysql的jquery datepickers中禁用给定范围内的日期

时间:2017-04-29 13:53:01

标签: javascript php jquery mysql datepicker

这是我的第一篇文章,请原谅我的任何错误

我是JQUERY和AJAX的新手,所以请非常清楚地说明你的答案

我做了很多搜索,但无济于事, 我正在使用PHP MYSQLI和JQUERY构建公寓预订系统, 除了签到日期之外,我在同一页面上有2个jquery datepickers,除了结账日期之外, 使用的MYSQL表称为预订,它有这些列 -

id,first_name,email,check_in_date,check_out_date

让我们说Mysql表返回2行,第一行返回check_in_date(2017-05-02)和check_out_date(2017-05-10)

和第二次返回check_in_date(2017-06-05)和check_out_date(2017-06-20)

如何在两个日期选择器上禁用这两个范围内的所有日期 这就是我提出的问题

这是HTML表单的一部分

<div class="form-group">
<label for="check_in_date">Check In Date</label>
<input type="text" class="form-control" id="datepicker" placeholder="Check in Date">
</div>
<div class="form-group">
<label for="check_out_date">Check Out Date</label>
<input type="text" class="form-control" id="datepicker1" placeholder="Check Out Date">
</div>

这是Javascript - 它的不完整

$(function() {

  function checkAvailability(){

}

$( "#datepicker" ).datepicker({
    dateFormat : 'yy-mm-dd',
    beforeShowDay: checkAvailability        
    }       
});

$( "#datepicker1" ).datepicker({
    dateFormat : 'yy-mm-dd',
    beforeShowDay: checkAvailability

    }       
});

  });

PHP文件availability.php如下

$result = mysqli_query("SELECT check_in_date, check_out_date FROM bookings");

while ($row = mysqli_fetch_assoc($result)){

 $check_in_dates[] =  $row['check_in_date'];

 $check_out_dates[] =  $row['check_out_date'];  
 }

先谢谢

1 个答案:

答案 0 :(得分:0)

我在这里想出来的是解决方案

    <?php
    //below function returns all the dates within a given range
     function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {//if it was simple y example y-m-d then it will show 17 instead of 2017

       $dates = array();
       $current = strtotime($first);
       $last = strtotime($last);

while( $current <= $last ) {

    $dates[] = date($output_format, $current);
    $current = strtotime($step, $current);
}//end of while loop

    return $dates;//returns a array
   }//end of function
   //select all the  date ranges that exist in the database 
     $query = 'SELECT check_in, check_out FROM bookings';

 $result = mysqli_query($db, $query);

    $i = 0;
    while ($row=mysqli_fetch_assoc($result)){
    //output all the dates within the ranges in the database
    $range[$i] = date_range($row['check_in'], $row['check_out']);//creates a associative array with numerical index values



  $i++;     


  }

  $individual_dates = array();

 //converts the associative array  into a regular array
  foreach($range as $ranges){
     foreach ($ranges as $many_ranges){
       $individual_dates[] = $many_ranges;
     }
  }


 $json_array = json_encode($individual_dates);

 ?>

这里是javascript

<script>
            var dateToday = new Date(); //creating new date obj. used to disable dates before today used in minDate: dateToday
              $(function() {

                $( "#datepicker" ).datepicker({
                    minDate: dateToday,     
                        dateFormat : 'yy-mm-dd',
                    beforeShowDay: checkAvailability

                    });

                $( "#datepicker1" ).datepicker({
                        minDate: dateToday,
                        dateFormat : 'yy-mm-dd',
                    beforeShowDay: checkAvailability        
                    });

              })
              /******************THE json array returned by php is used here*****/
                  var $disabledDates = <?php echo $json_array; ?>

                function checkAvailability(mydate){
                var $return=true;
                var $returnclass ="available";
                $checkdate = $.datepicker.formatDate('yy-mm-dd', mydate);
                for(var i = 0; i < $disabledDates.length; i++)
                    {    
                       if($disabledDates[i] == $checkdate)
                          {
                        $return = false;
                        $returnclass= "unavailable";
                        }
                    }
                return [$return,$returnclass];
                }

              </script>