我从about.com获得了上述代码。除2015年8月外,一切运作良好。开始日期应为星期六,但日历显示星期一。我到目前为止检查过的其他几个月都是正确的。
任何提示?
<?php
function calendar() {
date_default_timezone_set('UTC');
$date = time(); // it was removed orginally.
if (ISSET($_REQUEST['emonth'])&& ISSET($_REQUEST['eyear'])){
$month = $_REQUEST['emonth'];
$year = $_REQUEST['eyear'];
} else {
$month = date('m', $date);
$year = date('Y', $date);
}
$first_day = mktime(0,0,0,$month, 1, $year);
echo date('D', $first_day);
$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 "Sar": $blank = 6; break;
}
$days_in_month = cal_days_in_month(0, $month, $year);
echo '<table class="event">
<tr>
<td colspan="7">'.$title.'-'.$year.'</td>
</tr><tr>
<th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th>
</tr>';
$day_count = 1;
echo '<tr>';
while ($blank > 0) {
echo '<td></td>';
$blank = $blank-1;
$day_count++;
}
$day_num = 1;
while ($day_num <= $days_in_month){
echo "<td>$day_num</td>";
$day_num++;
$day_count++;
if ($day_count > 7) {
echo '</tr><tr>';
$day_count = 1;
while ($day_count > 1 && $day_count <= 7) {
echo '<td></td>';
$day_count++;
}
}
}
echo '</tr>
</table>';
}
calendar();
?>
解决
在切换案例中,有一个字形的Sar而不是Sat。由于它在我检查过的大部分时间里都有效,我忽略了它。它现在在纠正之后有效。
谢谢你们
答案 0 :(得分:1)
您没有定义$date
- 尽管在您的示例中它甚至没有必要。如果没有传入第二个参数,date()
函数将默认为今天。(同样isset
可以将逗号分隔的变量列表更清晰)
if (isset($_REQUEST['emonth'],$_REQUEST['eyear'])){
$month = $_REQUEST['emonth'];
$year = $_REQUEST['eyear'];
} else {
$month = date('m');
$year = date('Y');
}
另外,正如评论中所提到的,你的switch语句中有一个拼写错误会阻止“星期六”的匹配。