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

php calendar: adding days of week from previous month to first week

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

上图是三月份的。看起来不错,但是当我选择 August 时,它看起来像这样:

(每个方框里的两个数字是:上面一个:天。下面一个:我输入的另一个数字没有任何意义)

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

我该如何解决这个问题?

这是我的代码:

<?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>";
?>

或者有没有更好的方法将上个月的月底添加到新日历的开头?下个月到这个日历的末尾也是一样吗?

你必须换行

$last_month_days_to_add_to_last_month_end = $blank++;

$last_month_days_to_add_to_last_month_end = $blank;
<?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; 
 } 





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;




$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>"; 
switch($blank){ 
case "0": 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>";  break; 
case "1": echo "<tr><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><td width=42>S</td></tr>"; break; 
case "2":  echo "<tr><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><td width=42>S</td><td width=42>M</td></tr>"; break; 
case "3":  echo "<tr><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td></tr>"; break; 
case "4":  echo "<tr><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td></tr>"; break; 
case "5": echo "<tr><td width=42>F</td><td width=42>S</td><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></tr>"; break; 
case "6": echo "<tr><td width=42>S</td><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></tr>";  break; 

default: 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></tr>"; 
 } 

          //This counts the days in the week, up to 7 
$day_count = 1; echo "<tr>"; 





$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;'> - </span></td>";   
$day_count++;   
$end_days++;
}   
echo "</tr></table>";
?>