I am having an booking application in which i have to disable the already booked date in date range picker

Issue

date range in html form

 <div class="form-group " id="input-dates">
<input class="form-control date-range-picker" id="dateRange" type="text" name="dates" placeholder="<?php echo get_phrase('when'); ?>.." autocomplete="off" required>
<i class="icon_calendar"></i>
</div>

I have to pass the date range which are coming from mysql database to date range picker to disable them

function myCallback(start, end) {
  console.log("callback");

  $("#dateRange_n").html(
    start.format("MM DD, YYYY") + " - " + end.format("MM D, YYYY")
  );
}

let options = {
  startDate: "12-01-2018",
  endDate: "12-01-2023",
  minDate: "10-06-2021",
  isInvalidDate: function(ele) {
    var compareDate = moment(ele);
    var startDate = moment("12/01/2019", "DD/MM/YYYY");
    var endDate = moment("5/06/2021", "DD/MM/YYYY");
    return compareDate.isBetween(startDate, endDate);
  },
};

$("#dateRange_n").daterangepicker(options, myCallback);

<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"> 
</script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"> 
   </script>
<script type="text/javascript" 
src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" 
href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<div class="form-group" id="input-dates">
  <input class="form-control date-range-picker" id="dateRange_n" type="text" name="dates" 
placeholder="test...." autocomplete="off" required />
  <i class="icon_calendar"></i>
 </div>

i get the dates form database as follows:

<?php  
$this->db->select('booking_date');
  $this->db->where('listing_id', $listing_details['id']);
  $records = $this->db->get('booking');
    $res = $records->result_array();  ?>

where i get the date range as follows

Array ( [0] => Array ( [booking_date] => 2021-06-05 - 2021-06-06 ) [1] => Array ( [booking_date] => 2021-06-15 - 2021-06-16 ) [2] => Array ( [booking_date] => 2021-06-17 - 2021-06-18 ) [3] => Array ( [booking_date] => 2021-06-19 - 2021-06-20 ) )

Solution

Use Litepicker for this(https://litepicker.com/).

Init Litepicker with this params:

      disallowLockDaysInRange: true,
      lockDays: locked,
      highlightedDays: locked,

"locked" var example:

var locked = [["2021-09-25","2021-09-29"],["2021-10-11","2021-10-14"],["2021-10-16","2021-10-26"],["2021-10-27","2021-10-31"],["2021-07-05","2021-07-10"],["2021-07-26","2021-08-01"],["2021-08-02","2021-08-11"],["2021-09-05","2021-09-10"],["2021-08-26","2021-09-01"],["2021-11-12","2021-11-19"],["2021-02-25","2021-04-25"],["2021-09-14","2021-09-24"],["2021-07-11","2021-07-25"],["2021-08-12","2021-08-18"]];

Answered By – marts

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.