Адаптиране на моя календар, за да работи за дати през 2 месеца

Работя върху календар, използвайки собствен код, в момента мога да получа дати, които да се показват като 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. Това означава, че календарът ще работи само за дати, които се съдържат в рамките на същия месец...

Как да накарам датите да работят през месеците?


person 099    schedule 17.06.2015    source източник


Отговори (1)


В крайна сметка преработих изцяло моята календарна система, за да накарам това да работи по начина, по който исках:

//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>
person 099    schedule 18.06.2015