php日历:从上个月到第一周添加星期几

时间:2015-03-11 16:40:08

标签: php calendar

我有一个用PHP制作的日历。它看起来像这样:

Calendar

上面的图片是3月份的图片。它看起来很好,但是当我选择八月它看起来像这样:

Calendar of August that is bad

(每个方框中的两个数字是:前一个:天。下一个:我放入的另一个数字并不意味着什么)

如果您注意到第一天的右侧,它甚至不在日历上。

我该如何解决这个问题?

这是我的代码:

<?php
$date = time() ;

$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);

$first_day = mktime(0,0,0,$month, 1, $year) ;

$title = date('F', $first_day) ;

$day_of_week = date('D', $first_day) ;
switch($day_of_week){ 
case "Sun": $blank = 0; break; 
case "Mon": $blank = 1; break; 
case "Tue": $blank = 2; break; 
case "Wed": $blank = 3; break; 
case "Thu": $blank = 4; break; 
case "Fri": $blank = 5; break; 
case "Sat": $blank = 6; break; 
 } 

$days_in_month = cal_days_in_month(0, $month, $year) ;

echo "<table border=1 width=294>"; 
echo "<tr><th colspan=7> $title $year </th></tr>"; 
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td     width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";            //This counts the days in the week, up to 7 
$day_count = 1; echo "<tr>"; 



if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank++;
$last_month_end = $last_month_days_in_month-    $last_month_days_to_add_to_last_month_end;



while ( $blank > 0 ) { 

echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>"; 
$last_month_end++;
$blank = $blank-1; 
$day_count++; 
}

$day_num = 1; 

while ( $day_num <= $days_in_month ) { 
echo "<td><a href='/test/day.php?day=" . $day_num . "&month=" . $title . "&year=" . $year . "'>" . $day_num . '<br>';
echo "</a></td>"; 
$day_num++;
$day_count++; 

if ($day_count > 7) { 
echo "</tr><tr>"; 
$day_count = 1; 
} 
}

$end_days = 1;
while ( $day_count >1 && $day_count <=7 )   {   
echo "<td><span style='color:grey;'>" . $end_days . "</span></td>";   
$day_count++;   
$end_days++;
}   
echo "</tr></table>";
?>

还是有更好的方法将上个月的结尾添加到新日历的开头?并且下个月到这个日历结束时一样吗?

2 个答案:

答案 0 :(得分:1)

您必须更改行

$last_month_days_to_add_to_last_month_end = $blank++;

$last_month_days_to_add_to_last_month_end = $blank;

答案 1 :(得分:0)

<c:forEach items="${books}" var="list">
    <tr>
        <td>ok : ${list.id}</td>
        <td><c:out value="${list.name}" /></td>
        <td><c:out value="${list.description}" /></td>
        <td><fmt:formatNumber value="${list.price}" type="currency" /></td>
    </tr>
</c:forEach>