title: Disabling dates in date picker from MySQL
body: <p>I am fairly new to JavaScript and through few tutorials have been able to put the following code together for the jQuery date picker. However, I need to be able to store disabled dates in a variable which will be the result of a query. </p>
  
  <p>I also need to prevent someone from booking through disabled dates, therefore if 3/3/15 is disabled someone can't select from 2/3/15 to 3/3/15. </p>
  
  <p>Below is the code which I have in my JS file. Any help would be appreciated.</p>
  
  <pre><code>$(document).ready(function() {
  
  var bookedDays = ["2015-2-27","2015-2-28","2010-6-14"];
  ***Should be results of select from table query. 
  e.g SELECT fromdate, todate from table where id = $_GET['id']***
  
  
      $('#request').hide();
      $('.days').html('Please select a date range of at least the same day. &lt;br/&gt; &lt;i&gt;Max booking: 2 Months.&lt;/i&gt;');
      $( "#from" ).datepicker({
        defaultDate: new Date(),
        changeMonth: true,
        numberOfMonths: 1,
        minDate: new Date(),
        maxDate: "+1M",
        beforeShowDay: isAvailable,
        onClose: function( selectedDate ) {
          var day = $("#from").datepicker('getDate');
  day.setDate(day.getDate()+1);
          $( "#to" ).datepicker( "option", "minDate", day );
  
        }
      });
  
      $( "#to" ).datepicker({
  
        defaultDate: new Date(),
        changeMonth: true,
        numberOfMonths: 1,
        minDate: ("#to"),
        maxDate: "+2M",
        beforeShowDay: isAvailable,
        onClose: function( selectedDate ) {
          $( "#from" ).datepicker( "option", "maxDate", selectedDate );
        }
      });
  
      function isAvailable(date){
      var dateAsString = date.getFullYear().toString() + "-" + (date.getMonth()+1).toString() + "-" + date.getDate();
      var result = $.inArray( dateAsString, bookedDays ) ===-1 ? [true] : [false];
      return result;
    }
  
  
      $('#to').on('change',function(){
       var days = (daydiff(parseDate($('#from').val()), parseDate($('#to').val())));
        var cogs = $('#cogsday').html();
        cogs = cogs.replace(/\D/g,'');
        var x = days;
        var y = cogs * x;
        $('.days').html('You have chosen to borrow this item for &lt;b&gt;'+ days + '&lt;/b&gt; days at a cost of &lt;b&gt;' + y + '&lt;/b&gt; cogs.&lt;br/&gt;&lt;br/&gt;');
        if(days){
          $('#request').show();
        }
        $('#request').click(function() {
  
                  var cogs = $('#cogsday').html();
  
                  cogs = cogs.replace(/\D/g,'');
  
          var x = days ;
  
          var y = cogs * x;
          $('#total').text(y);
          $('#nameID').val(y);
          $('#days').text(days);
          $('#daysID').val(days);
  
          });
      })
  
      function parseDate(str) {
      var mdy = str.split('/')
      return new Date(mdy[2], mdy[0]-1, mdy[1]);
  }
  
  function daydiff(first, second) {
      return Math.round((second-first)/(1000*60*60*24));
  }
  
    });
  </code></pre>