php - allow datepicker to pick values only upto two weeks from the start date

371
<script type="text/javascript">
$(function() {
   $('#downpayment_date').datepicker({
       dateFormat: 'm/d/yy', minDate: new Date(<?php echo $this->y;?>, <?php echo (($this->m) - 1);?>, <?php echo ($this->d);?>),
       beforeShowDay: $.datepicker.noWeekends
   });
});
</script>

Okay I have this datepicker that allows start date based on the value retrieved dynamically in PHP.

Is it possible to limit the selection of date to just two weeks from the startdate.

.i.e if the start date in the above datepicker starts from 08/12/2012, the datepicker should allow me to only select dates upto two weeks from that date.

      $('#downpayment_date').datepicker({
           dateFormat: 'm/d/yy', minDate: new Date(<?php echo $this->y;?>, <?php echo (($this->m) - 1);?>, <?php echo ($this->d);?>),
           maxDate:new Date(<?php echo $this->y;?>, <?php echo (($this->m) - 1);?>, <?php echo ($this->d)+14;?>),
           beforeShowDay: $.datepicker.noWeekends
       });
843

Answer

Solution:

Probably the easiest thing would be to pass in the PHP date and just add 14 days to it like this:

var start = '08/12/2012';
var dateArray = start.split('/');
$('#date').datepicker({
    minDate: new Date(dateArray[2],dateArray[0]-1, dateArray[1], 0, 0, 0, 0),
    maxDate: new Date(dateArray[2],dateArray[0]-1, parseInt(dateArray[1])+14, 0, 0, 0, 0)
});

This handles dates across months and years just fine. Here's a jsFiddle example. So instead of hard coding the start date as I do above, just echo the PHP date as you noted.

898

Answer

Solution:

Use the maxDate attribute in the date picker and set it to minDate + 14

620

Answer

Solution:

$(document).ready(function() {
    var MyMaxDate = $("#date" ).datepicker( "option", "minDate" );
    $( "#date" ).datepicker({ minDate: new Date(<?php echo $y;?>, <?php echo ($m - 1);?>, <?php echo $d;?>),maxDate: MyMaxDate +14, beforeShowDay: $.datepicker.noWeekends });
});

Actually I found the above method bit more efficient and faster.

People are also looking for solutions to the problem: php - How to disable query logging in console while load Doctrine fixtures?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.