我的网站上有一个使用PHP / HTML的日历,它工作正常,但由于某种原因,它显示了今天的日期之前的最后5天,理想情况下我希望它们被隐藏。这是我的PHP& CSS代码:
.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">« <a href="calendar.php?month=-1">Previous Month</a></div>
<div class="floatRight"><a href="calendar.php?month=+1">Next Month</a> »</div>
<div class="clearBoth"></div>
</div>
';
?>
有谁能告诉我如何阻止今天之前的所有事情?
另外,正如你所看到的,我希望有一个下一个&amp;前几个月的导航,但我不知道如何让导航刷新PHP脚本与相关的月份信息。对此的任何帮助也将非常感激
答案 0 :(得分:0)
为什么你只是这样做?
$currDay = date("j"); //at top of your code
然后循环。
if((int)$currDay > (int)$date) {
$class ='calendarBlank';
}
这样做。
PS。 Lemme知道你是否需要完整的代码。