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

Php 如何显示两个给定日期之间的所有日期?,php,Php,我正在创建一个日历,在其中我必须标记两个给定日期之间的所有日期。但这些日期不是静态的,它们是从数据库中获取的。例如: <?php $dateYear = ($year != '')?$year:date("Y"); $dateMonth = ($month != '')?$month:date("m"); $date = $dateYear.'-'.$dateMonth.'-01'; $currentMonthFirstDay = date("N",strtotime($date)); $

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

<?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>

  • 周一
  • 星期二
  • 结婚
  • 周四
  • 星期五
  • 太阳

    像这样的
    如果($date>$startDate&$date<$endDate){/*在此处更改颜色*/}
    其中
    $startDate
    $endDate
    是从数据库中提取的两个日期,我将尝试。谢谢对于提示,对于所涉及的任务,您的代码似乎非常复杂。本质上,您所做的只是获取一个日期列表,然后给定时间段之间的任何日期都会以不同的颜色显示。但是由于某些原因,您声明了很多变量和代码块。当我有五分钟的空闲时间时,我会试着为你减少这一点。是的,最好的代码是最简单的。我真的不知道为什么我不能通过这一次。但这几个月的经历有一个问题。正如你所看到的,我有两个链接。有了这些链接,我可以切换到下个月或上个月。但是我不能,因为onclick不起作用。为什么?你问?因为在“getCalendar()”之前我需要一个指针“$this->”,但我似乎无法将其包括在内,因为当我包括标记时,onclick中的所有内容都会发生故障。这段代码中有很多问题,但现在我想解决这个问题。嘿,你的建议是我一直在寻找的答案,但包括它实际开始的日期和结束的日期应该是“>=”而不是“>”和“