如何显示两个给定日期之间的所有日期?

时间:2018-05-31 10:26:05

标签: php

我正在创建一个日历,我必须在其中标记两个给定日期之间的所有日期。但是日期不是静态的,它们来自数据库。例如:

<?php
$dateYear = ($year != '')?$year:date("Y");
$dateMonth = ($month != '')?$month:date("m");
$date = $dateYear.'-'.$dateMonth.'-01';
$currentMonthFirstDay = date("N",strtotime($date));
$totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);
$totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);
$boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;
?>
<div id="calender_section">
    <h2>
        <a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' - 1 Month')); ?>','<?php echo date("m",strtotime($date.' - 1 Month')); ?>');">&lt;&lt;</a>
        <select name="month_dropdown" class="month_dropdown dropdown"><?php echo $this->getAllMonths($dateMonth); ?></select>
        <select name="year_dropdown" class="year_dropdown dropdown"><?php echo $this->getYearList($dateYear); ?></select>
        <a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' + 1 Month')); ?>','<?php echo date("m",strtotime($date.' + 1 Month')); ?>');">&gt;&gt;</a>
    </h2>
    <div id="event_list" class="none"></div>
    <div id="calender_section_top">
        <ul>
            <li>Mon</li>
            <li>Tue</li>
            <li>Wed</li>
            <li>Thu</li>
            <li>Fri</li>
            <li>Sat</li>
            <li>Sun</li>
        </ul>
    </div>
    <div id="calender_section_bot">
        <ul>
            <?php 
            $dayCount = 1; 
            for ($cb = 1; $cb <= $boxDisplay; $cb++) {
                if (($cb >= $currentMonthFirstDay || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay - 1)) {
                    // Current date
                    $currentDate = $dateYear.'-'.$dateMonth.'-'.$dayCount;
                    $eventNum = 0;
                    // Include db configuration file
                    // Get number of events based on the current date
                    $sql = "SELECT date_from, date_to FROM booking_2";
                    $result = $this->connect()->query($sql); 
                    /*die(var_dump($result));*/
                    $eventNum = $result->num_rows;
                    if ($eventNum > 0) {
                        while ($row = $result->fetch_assoc()) {
                            $date_from = $row['date_from'];
                            $date_to = $row['date_to'];
                            $time_from = strtotime($date_from);
                            $time_fromm = idate('d', $time_from);
                            $time_to = strtotime($date_to);
                            $time_too = idate('d', $time_to);
                        }
                        $period = new DatePeriod(
                            new DateTime($date_from),
                            new DateInterval('P1D'),
                            new DateTime($date_to)
                        );

                        if ($time_from == $currentDate) {
                            /*echo '<li style="background-color:#FF3232 !important;" date="'.date("Y-m-d", strtotime($currentDate)).'" class="date_cell"><span>'.date("d", strtotime($currentDate)).'</span>';  */
                            foreach ($period as $key) {
                                echo '<li style="background-color:#FF3232 !important;"'.$key->format('Y-m-d');    
                            }
                        } else {
                            echo '<li date="'.$currentDate.'" class="date_cell">';
                        }
                    }
                    // Date cell
                    echo '<span>';
                    echo $dayCount;
                    echo '</span>';

                    echo '</li>';
                    $dayCount++;
                    ?>
            <?php } else { ?>
            <li><span>&nbsp;</span></li>
            <?php } } ?>
        </ul>
    </div>
</div>

如何使从数据库中提取的日期标记为其他颜色?我已经尝试过for循环并且它主要起作用但是它再次最终仅从数据库中获取最后一行,我需要日历来显示所有采用日期而不仅仅是数据库中的最后一行

0 个答案:

没有答案
相关问题