我从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。因为它在我检查的大部分月份都有效,所以我忽略了它。
谢谢人
您没有定义$date
-尽管在您的示例中甚至没有必要。如果没有传入第二个参数,date()
函数将默认为今天。(isset
也可以采取逗号分隔的变量列表,以更干净)
if (isset($_REQUEST['emonth'],$_REQUEST['eyear'])){
$month = $_REQUEST['emonth'];
$year = $_REQUEST['eyear'];
} else {
$month = date('m');
$year = date('Y');
}
另外,正如在评论中提到的,你的switch语句中有一个错别字,这将阻止'Sat'的匹配。