0

I have a javascript jquery date picker which, at the minute only allows you to select days based on days in a database, as well as only allowing you to select the current day (if it isnt in the database as a holiday). Weekends are also disabled.

What i'm trying to do now is only allow the user to select a day from database +1 on date picker.

I've obviously pulled out the latest date entered into my database and plus'd one to it, so this is the date i only want to be available to select... But what if the next day is a disabled day from the other Holiday dates? which way can i get around this and actually only displaying one? AND having a condition on my holiday dates?

My javascript jquery date picker atm:

 <script type="text/javascript">

     //  Holiday Dates
    var unavailableDates = 
[<?php 

while($date = mysqli_fetch_assoc($execute)) {

        //  Populating javascript array with the dates from database
        echo '"' . $date = date('Y-n-j', strtotime($date['holiday_date'])) . '"';

        //  Adding a comma in array
    if ( $i < $number) {
                    echo ',';
    }
    $i ++;
}
    ?>
    ];

//  Entered Dates
var unavailableEnteredDates = 
[<?php 

while($dateEntered = mysqli_fetch_assoc($executeQuery)) {
    //  Populating javascript array with the dates from database
    echo '"' . $dateEntered = date('Y-n-j', strtotime($dateEntered['totalsDate'])) . '"';

    //  Adding a comma in array
    if ( $y < $number2) {
        echo ',';
    }
    $y ++;
}
?>
];

//  Concatenented array of 2 above
var allUnavailableDates = unavailableDates.concat(unavailableEnteredDates);

    //  Sets the no weekends and specified holiday dates
   function noWeekendsOrHolidays(date) {
    var noWeekend = jQuery.datepicker.noWeekends(date);
    return noWeekend[0] ? holidays(date) : noWeekend;
}

//  Gets the holidays
function holidays(date) {
    dmy = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    if ($.inArray(dmy, allUnavailableDates) == -1) {
        return [true, ""];
    } else {
        return [false, "", "Holiday Date"];
    }

}

//  Setting up the Date Picker
$(function() {
    $("#datepickerUser").datepicker({
        dateFormat: 'yy-m-d',
        beforeShowDay: noWeekendsOrHolidays,
        maxDate: "+0d"
    });
    $(function() { ("#testButton").datepicker("show") });

});

My solution i came up with:

          //    Adding one to the day so we can display this
      $latestPlusOne = date('Y-n-j', strtotime( "$latestDate + 1 day" ));
      $availableTakingDate;

        //  This for loop will loop through the size of the array
        //  Each time it finds the date inside the array it will plus 1 to the date
        //  Otherwise, break out of the loop and the new date var will be an available one
for ($i = 0; $i <= count($dateArray); $i++) {
    if (in_array($latestPlusOne, $dateArray)) {

        $latestPlusOne = date('Y-n-j', strtotime( "$latestPlusOne + 1 day" ));
    } else {
        $availableTakingDate = $latestPlusOne;
        break;
    }
}
?>


<script type="text/javascript">

    //  Date :: wanted available
    var availableDate = <?php echo json_encode($availableTakingDate); ?>;

function displayDate(date) {
    dmy = date.getFullYear() + "-" + (date.getMonth() +1) + "-" + date.getDate();

    if (dmy == availableDate) {
        return [true, ""];

    } else {
        return [false, ""];
    }
}

//  Setting up the Date Picker
$(function() {
    $("#datepickerUser").datepicker({
        dateFormat: 'yy-m-d',
        beforeShowDay: displayDate,
        maxDate: "+0d"
    });
    $(function() { ("#testButton").datepicker("show") });

});

2
  • you should paste a minimum of code so users can help you with your problem. Commented Sep 17, 2014 at 21:36
  • thanks, ive added my javascript Commented Sep 17, 2014 at 21:38

1 Answer 1

1

There is no way datepicker would ever know holidays in your country. You need to implement your own programming logic - define holidays in your country though the year and calculate next day. While next day is found on holiday list, expand dateTo +1 day.

Sign up to request clarification or add additional context in comments.

4 Comments

I have implemented my own holiday system, hence "From the database" lol.
Yes, but you need to implement it into an option "maxDate". Instead of maxDate: "+0d" use maxDate: "+1" or "+2" etc. maxDate allows numeric value, or Date object, which is more suitable in your case than string argument "+0d". See api.jqueryui.com/datepicker/#option-maxDate .
Got it, I used a for loop in php to check my Date+1 against an array of unavailable dates, if it exists +1 more to my Date. Then just in beforeShowDay used a function to set the Date to my variable from PHP. Thanks! Putting code in main post to see
Nice. Just one little note - I would not recommend using strtotime() since it is locally formating-aware function, meaning it may work on one setup but may not work on another server - depending on envorinmental localle setting. PHP's mktime(...) is definitely better way to go. See how many confusion have developers about strtotime() in comments at the bottom: php.net/manual/en/function.strtotime.php .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.