Blog

06 Dec
2012

jQuery Icon Logo

jQuery datepicker disable dates


Here is a nifty little script for disabling dates, if you are using the jQuery datepicker.

One thing to remember all the days and months must be a single digit so the 1st of February 2012 should be 1-2-2012 NOT 01-02-2012

<script type="text/javascript">
    $(document).ready(function(){

    var unavailableDates = ["26-12-2012","27-12-2012","28-12-2012","29-12-2012","30-12-2012","31-12-2012","1-1-2013","2-1-2013","3-1-2013","4-1-2013","5-1-2013","6-1-2013"];

    function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if ($.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Not Available"];
        }
    }

    $('#date_output').datepicker({ beforeShowDay: unavailable, dateFormat: 'dd-mm-yy', changeFirstDay: false, firstDay: 1, minDate: 0, onSelect: function(dateStr) {
    }});

    });
    </script>

If you also want to be able to output the date range using php then this should help.

<?php
function dateRange( $first, $last, $step = '+1 day', $format = 'j-n-Y' ) {

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

    while( $current <= $last ) {

        $dates[] = date( $format, $current );
        $current = strtotime( $step, $current );
    }

    return $dates;
}
// Example usage
print_r( dateRange( '2010/07/26', '2010/08/05') );
?>
Posted in: < PHP and HTML and CSS >
me@grafxflow avatar

me@grafxflow


I am a Full-stack Developer who also started delving into the world of UX/UI Design a few years back. I blog and tweet to hopefully share a little bit of knowledge that can help others around the web. Thanks for stopping by!

Visitors also viewed these posts

2 Thoughts

  1. 21 Dec
    2018

    avatar

    genasky

    how to disable date from my database using php
  2. 28 Dec
    2018

    avatar

    me@grafxflow

    Hi Genasky,

    Unsure if I know exactly what you mean.

    Could you go into more detail?

Add comment

Add comment