关于php/javascript中的日期选择器

Regarding a Date Picker in php/javascript

本文关键字:日期 选择器 javascript php 关于      更新时间:2023-09-26

我有一个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") });
});

日期选择器不可能知道您所在国家的假期。你需要实现自己的编程逻辑——定义一年中你所在国家的假期,并计算第二天。当在假日列表中找到第二天时,将dateTo+1天展开。