【发布时间】:2014-09-17 21:35:28
【问题描述】:
我有一个 javascript jquery 日期选择器,它只允许您根据数据库中的天数选择天数,并且只允许您选择当前日期(如果它不在数据库中作为假期) .周末也被禁用。
我现在要做的是只允许用户在日期选择器上从数据库 +1 中选择一天。
我显然已经提取了输入到我的数据库中的最新日期并加了一个,所以这是我只想选择的日期...但是如果第二天是禁用日怎么办从其他假期日期开始?我可以通过哪种方式解决这个问题,实际上只显示一个?并且在我的假期日期有条件?
我的 javascript jquery 日期选择器 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") });
});
我想出的解决方案:
// 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") });
});
【问题讨论】:
-
您应该粘贴最少的代码,以便用户可以帮助您解决问题。
-
谢谢,我添加了我的 javascript
标签: javascript php jquery html datepicker