jQuery Icon Logo
me@grafxflow

Written by me@grafxflow

06 Dec, 2012

2

5,385

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') );
?>

Add comment

2 Response

    genasky
    21 Dec 2018

    how to disable date from my database using php

    |

    me@grafxflow
    28 Dec 2018

    Hi Genasky,

    Unsure if I know exactly what you mean.

    Could you go into more detail?

Smart Search

133 Following
50 Followers

me@grafxflow

Hull, United Kingdom

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!

Follow