调整“我的日历”以适用于 2 个月的日期


Adapting My Calendar to work for dates across 2 months

我正在使用自己的代码处理日历,如果日期在同一个月内,我目前只能获得显示为booked的日期。

如果日期跨越超过一个月,我将如何让事情发挥作用?

这些是设置日历的主要变量(在我的while循环之前):

//Labels
$dayLabels          = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$dayMiniLabels      = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
$monthLables        = array("January","February","March","April","May","June","July","August","September","October","November","December");
//max values
$maxDays                = 7;
$maxMonths          = 12;
//stats
$forceMonth             = $_GET['m'];
$forceYear          = $_GET['y'];
$todayDate          = date("d-m-Y");
$todayDate          = date("d-m-Y", strtotime($todayDate));
$explodeToday       = explode("-", $todayDate);
$currentDay         = $explodeToday[0];
if(isset($forceMonth)) {
    $currentMonth   = $forceMonth;
} else {
    $currentMonth   = $explodeToday[1];
};
if(isset($forceYear)) {
    $currentYear        = $forceYear;
} else {
    $currentYear        = $explodeToday[2];
};
$currentDate            = strtotime("01-$currentMonth-$currentYear");
$prevMonth          = sprintf("%02d", $currentMonth - 1);
$nextMonth          = sprintf("%02d", $currentMonth + 1);
$prevYear           = sprintf("%02d", $currentYear - 1);
$nextYear           = sprintf("%02d", $currentYear + 1);
$daysInMonth        = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
$firstDayofMonth    = date("D", $currentDate);
$firstDayofMonth    = array_search($firstDayofMonth, $dayMiniLabels);
$firstDayofMonth    = $firstDayofMonth;
//database values
$bookedStart            = array();
$bookedEnd          = array();
$bookedUser         = array();
if($getBookings = $con->prepare("SELECT userID,bookingStart,bookingEnd FROM bookings WHERE machineID=?")) {
    $getBookings->bind_param("i", $_GET['id']);
    if($getBookings->execute()) {
        $getBookings->bind_result($bookingUserID,$bookingStart,$bookingEnd);
        while($getBookings->fetch()) {
            array_push($bookedStart,    $bookingStart);
            array_push($bookedEnd,  $bookingEnd);
            array_push($bookedUser,     $bookingUserID);
        };
    };
};
$getBookings->close();
//counters
$daysIntoMonth      = 0;
$dayCounter         = 0;
$startMonth         = 0;

这是日历(打印日历的内容)的当前显示代码:

<table class="full grid dayLabels">
    <tr>
    <?php
        foreach($dayLabels as $day) {
            echo '<td class="day"><p>' .$day. '</p></td>';
        };
    ?>
    </tr>
</table>
<table id="calendar" class="full grid calendar">
    <?php
        while($daysIntoMonth < $daysInMonth) {
            //days into month
            $daysIntoMonth++;
            $temp_intoMonth             = sprintf("%02d", $daysIntoMonth);
            $daysIntoMonth      = $temp_intoMonth;
            //days into week
            $dayCounter++;
            $temp_dayCounter    = sprintf("%02d", $dayCounter);
            $dayCounter             = $temp_dayCounter;
            //current calendar date
            $calDate                    = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear));
            $calTime                    = strtotime($calDate);
            $todaysNumber           = date('w', $timeCal);
            if($dayCounter == 1) {
                echo '<tr>';
            };
            if($firstDayofMonth != 7) {
                while($startMonth < $firstDayofMonth) {
                    echo '<td class="padding"></td>';
                    $startMonth++;
                    $dayCounter++;
                    $temp_dayCounter = sprintf("%02d", $dayCounter);
                    $dayCounter = $temp_dayCounter;
                };
            };
            if($startKey = in_array($calDate, $bookedStart, true)) {
                $booked = true;
                echo '
                    <td class="booked">
                        <p class="date">' .$daysIntoMonth. '</p>
                    </td>
                ';
            } else if(in_array($calDate, $bookedEnd, true)) {
                $booked = false;
                echo '
                    <td class="booked">
                        <p class="date">' .$daysIntoMonth. '</p>
                    </td>
                ';
            } else if($booked == true) {
                echo '
                    <td class="booked">
                        <p class="date">' .$daysIntoMonth. '</p>
                    </td>
                ';
            } else {
                echo '
                    <td>
                        <p class="date">' .$daysIntoMonth. '</p>
                    </td>
                ';
            };
            if($dayCounter == $maxDays) {
                echo '</tr>';
                $dayCounter = 0;
            };
        };
    ?>
</table>

我目前正在寻找一个开始日期,将变量booked变量设置为true,直到遇到end date。这意味着日历仅适用于同一个月内包含的日期...

我将如何使日期跨月工作?

我最终完全重新设计了我的日历系统,让它按照我想要的方式工作:

//Labels
$dayLabels          = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$dayMiniLabels      = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
$monthLables        = array("January","February","March","April","May","June","July","August","September","October","November","December");
$forceMonth = $_GET['m'];
$forceYear = $_GET['y'];
$currentDate            = date("Y-m-d");
$explodeDate        = explode("-", $currentDate);
//Currents
if(isset($forceMonth)) {
    if(strlen($forceMonth) == 1) {
        $forceMonth     = sprintf("%02d", $forceMonth);
    };
    $currentMonth   = $forceMonth;
} else {
    $currentMonth   = date("m");
};
if(isset($forceYear)) {
    if(strlen($forceYear) == 2) {
        $dt                 = DateTime::createFromFormat('y', $forceYear);
        $forceYear  = $dt->format('Y');
    };
    $currentYear        = $forceYear;
} else {
    $currentYear        = date("Y");
};
//variables
$monthStart             = date($currentYear. '-' .$currentMonth. '-01');
$monthEnd               = date($currentYear. '-' .$currentMonth. '-t');
$prevMonth          = sprintf("%02d", $currentMonth - 1);
$nextMonth          = sprintf("%02d", $currentMonth + 1);
$prevYear           = sprintf("%02d", $currentYear - 1);
$nextYear           = sprintf("%02d", $currentYear + 1);
$daysInMonth        = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
$firstDayofMonth    = date("D", strtotime("01-$currentMonth-$currentYear"));
$firstDayofMonth    = array_search($firstDayofMonth, $dayMiniLabels);
$firstDayofMonth    = $firstDayofMonth;
if($firstDayofMonth == 0) {
    $firstDayofMonth = 7;
};
$bookings           = array();
$s                      = new DateTime($monthStart);
$e                      = new DateTime("$monthEnd + 1 days");
$oneday                 = new DateInterval('P1D');
$dp                         = new DatePeriod($s, $oneday, $e);
foreach ($dp as $d) {
    $bookings[$d->format('Y-m-d')] = '';   
};
$sql = "SELECT userID
         , bookingStart
         , bookingEnd 
        FROM bookings 
        WHERE machineID = ?
          AND bookingStart < ?
          AND bookingEnd > ?";
$getBookings = $con->prepare($sql);
$getBookings->bind_param('iss', $_GET['id'], $monthEnd, $monthStart);
$getBookings->execute();
$getBookings->bind_result($uid, $bstart, $bend);
while ($getBookings->fetch()) {
    $s                  = new DateTime(max($bstart, $monthStart));
    $e                  = new DateTime(min($bend, $monthEnd));
    $e->modify('+1 day');
    $dp = new DatePeriod($s, $oneday, $e);
    foreach ($dp as $d) {
        $bookings[$d->format('Y-m-d')] = $uid;
    };
};
//counters
$dayCount   = 0;
$startMonth     = 0;
$calDate        = 0;
<table class="full grid dayLabels">
    <tr>
    <?php
        foreach($dayLabels as $day) {
            echo '<td class="day"><p>' .$day. '</p></td>';
        };
    ?>
    </tr>
</table>
<table id="calendar" class="full grid calendar">
    <?php
        foreach($bookings as $date) {
            $dayCount++;
            $calDate++;
            $calDate = sprintf("%02d", $calDate);
            if($dayCount == 1) {
                echo '<tr>';
            };
            if($firstDayofMonth != 7) {
                while($startMonth < $firstDayofMonth) {
                    echo '<td class="padding"></td>';
                    $startMonth++;
                    $dayCount++;
                    $temp_dayCount = sprintf("%02d", $dayCount);
                    $dayCount = $temp_dayCount;
                };
            };
            if($date != "") {
                if($getUserName = $con->prepare("SELECT userFirst,userLast,userEmail FROM users WHERE userID=?")) {
                    $getUserName->bind_param("i", $date);
                    $getUserName->execute();
                    $getUserName->bind_result($userFirst,$userLast,$userEmail);
                    while($getUserName->fetch()) {
                        echo '
                            <td class="booked">
                                <p>' .$calDate. '</p>
                                <p>' .$userFirst. ' ' .$userLast. '</p>
                            </td>
                        ';
                    };
                };
                $getUserName->close();
            } else {
                echo '
                    <td>
                        <p>' .$calDate. '</p>
                ';
                if(isset($_SESSION['userID']) && $_SESSION['userPerms'] > 0) {
                    echo '
                        <a class="bookNow"></a>
                        <form class="book" method="post" action="./action.php?a=book">
                            <input name="userID" type="hidden" value="' .$_SESSION['userID']. '" required />
                            <input name="machineID" type="hidden" value="' .$id. '" required />
                            <div class="options">
                                <a class="fa fa-close"></a>
                            </div>
                            <p>Start Date</p>
                            <input name="startDate" type="text" value="' .$calDate. '-' .$currentMonth. '-' .$currentYear. '" autofocus required />
                            <p>End Date</p>
                            <input name="endDate" type="text" value="' .$calDate. '-' .$currentMonth. '-' .$currentYear. '" required />
                            <p>Project Number</p>
                            <input name="projectNumber" type="number" placeholder="Project Number" required />
                            <button class="confirm" type="submit">Submit</button>
                        </form>
                    ';
                };
                echo '
                    </td>
                ';
            };
            if($dayCount == 7) {
                echo '</tr>';
                $dayCount = 0;
            };
        };
    ?>
</table>