duanbei7035 2013-12-11 20:22
浏览 52
已采纳

PHP日历 - 隐藏今天之前的日子

I have a calendar on my website using PHP/HTML and it works fine except that for some reason it is displaying the last 5 days previous to today's date when ideally I want them to be hidden. Here is my PHP & CSS code:

.calendarHeader{
    text-align: center;
    text-decoration: underline;
    color: #303030;
}
.calendarDates{
    margin: 30px auto auto auto;
    width: 280px;
}
.calendar{
    margin: 10px auto auto auto;
    width: 280px;
}
.calendarCell{
    float: left;
    width: 40px;
    color: #707070;
    text-align: center;
    padding: 5px 0px 5px 0px;
}
.calendarCell:hover{
    color: #000000;
}
.calendarBlank{
    color: #E9EAEB;
    cursor: default;
}
.calendarBlank:hover{
    color: #FFFFFF;
}
.calendarToday{
    color: #BC3415;
    font-weight: bold;
}

<?php
$today = date("d"); // Current day
$month = date("m"); // Current month
$displaymonth = date("F");
$year = date("Y"); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month

$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month

$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of  current month
$laststart = $start - 1; // Days of previous month in calander

$counter = 1;
$nextMonthCounter = 1;

echo '

    <div class="calendarHeader"><strong>'.$displaymonth.' '.$year.'</strong></div>
    <div class="calendarDates">
        <div class="calendarCell"><b>M</b></div>
        <div class="calendarCell"><b>T</b></div>
        <div class="calendarCell"><b>W</b></div>
        <div class="calendarCell"><b>T</b></div>
        <div class="calendarCell"><b>F</b></div>
        <div class="calendarCell"><b>S</b></div>
        <div class="calendarCell"><b>S</b></div>

        <div class="clearLeft"></div>
    </div>
    <div class="calendar">
    ';

    if($start > 5){ $rows = 6; }else {$rows = 5; }

    for($i = 1; $i <= $rows; $i++){
        for($x = 1; $x <= 7; $x++){             

            if(($counter - $start) < 0){
                $date = (($lastmonth - $laststart) + $counter);
                $class = 'calendarBlank';
            }else if(($counter - $start) >= $days){
                $date = ($nextMonthCounter);
                $nextMonthCounter++;

                $class = 'calendarBlank';

            }else if($counter <= $today){
                $date = ($counter - $start + 1);
                $class = 'calendarBlank';
            }else {
                $date = ($counter - $start + 1);
                if($today == $counter - $start + 1){
                    $class = 'calendarToday';
                }
            }                                       

            echo '<a href="events.php" target="_parent"><div class="calendarCell '.$class.'">'.$date.'</div></a>';

            $counter++;
            $class = '';
        }
    }

    echo '
    <div class="clearLeft"></div>
    </div>
    <div class="calendarNavigation">
        <div class="floatLeft">&laquo; <a href="calendar.php?month=-1">Previous Month</a></div>
        <div class="floatRight"><a href="calendar.php?month=+1">Next Month</a> &raquo;</div>
        <div class="clearBoth"></div>
    </div>
';
?>

Can anyone tell me how to block out everything previous to today's date?

Also, as you can see, I'd like to have a next & previous months navigation but I don't know how to get the navigation to refresh the php script with the relevant month information. Any help on this would be hugely appreciated too

  • 写回答

1条回答 默认 最新

  • duan010167787 2013-12-11 20:44
    关注

    Why dun you simply do this?

    $currDay = date("j"); //at top of your code
    

    and then in loop.

    if((int)$currDay > (int)$date) {
       $class  ='calendarBlank';
    }
    

    This will do.

    PS. Lemme know if you need complete code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算