我需要一些使用PHP和Div表生成Web日历的帮助。我成功地使用HTML表格标签创建日历,但这不符合XHTML。我需要将日历表转换为div表。我的代码如下,我花了一些时间来解决这个问题,但没有运气。有人可以查看下面的代码并告诉我,我做错了什么?在此先感谢您的帮助。
<?php
$date = time();
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$blank = 0;
$day_one = mktime(0,0,0, $month, 1, $year);
$title = date('F', $day_one);
$week_day = date('D', $day_one);
switch ($week_day) {
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_current_month = cal_days_in_month(0, $month, $year);
echo "<div class='cTable'>
<div class='cTableRow'>
<div class='cTableHead'><span style='font-weight: bold;'>$title $year</span></div></div>";
echo "<div class='cTableBody'>
<div class='cTableRow'>
<div class='cTableCell'><span style='font-weight: bold;'>Monday</span></div>
<div class='cTableCell'><span style='font-weight: bold;'>Tuesday</span></div>
<div class='cTableCell'><span style='font-weight: bold;'>Wednesday</span></div>
<div class='cTableCell'><span style='font-weight: bold;'>Thursday</span></div>
<div class='cTableCell'><span style='font-weight: bold;'>Friday</span></div>
<div class='cTableCell'><span style='font-weight: bold;'>Saturday</span></div>
<div class='cTableHead'><span style='font-weight: bold;'>Sunday</span></div>
</div></div>";
$day_count = 1;
echo "<div class='cTableBody'><div class='cTableRow'>";
while ($blank > 0) {
echo "<div class='cTableCell'></div>";
$blank = $blank-1;
$day_count++;
}
$day_num = 1;
while ($day_num <= $days_in_current_month) {
echo "<div class='cTableCell'>$day_num</div></div></div>";
$day_num++;
$day_count++;
if ($day_count > 7) {
echo "<div class='cTableBody'><div class='cTableRow'>";
$day_count = 1;
}
}
while ($day_count > 1 && $day_count <=7) {
echo "<div class='cTableCell'></div>";
$day_count++;
}
echo "</div></div></div>";
?>
答案 0 :(得分:1)
你的问题就在这里。只有在更改行
时才需要关闭divs
while ($day_num <= $days_in_current_month) {
echo "<div class='cTableCell'>$day_num</div>";
$day_num++;
$day_count++;
if ($day_count > 7) {
echo "</div></div><div class='cTableBody'><div class='cTableRow'>";
$day_count = 1;
}
}
也是为了获得第一个工作日的正确数字表示,您需要更改此
$week_day = date('w', $day_one);
因为你需要得到&#34; w&#34;而不是&#34; D&#34;