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") });
});