Reputation: 146
I have the following (simplified) code for creating a calendar in PHP (month view).
<?php
$d = date_Parse_from_format('Y-m-d',date('Y-m-d',strtotime($_GET['date'])));
// month from date
$month = $d['month'];
// year from date
$year = $d['year'];
// days in month
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year);
// first day
// if sun (7) => 0
// else +1
$firstDay = date('N',strtotime($year.'-'.$month.'-1'));
if($firstDay == 7){
$firstDay = 1;
}else{
$firstDay = $firstDay+1;
};
// last day
$lastDay = $days;
// start day = 1
$day = 1;
// start cell = 1
$cell = 1;
echo '<table border="1" width="700">';
echo '<tr>';
echo '<td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td>';
echo '</tr>';
for($row=0;$row<ceil((($lastDay+$firstDay)-1)/7);$row++){
echo '<tr>';
for($col=1;$col<=7;$col++){
if($day > $days){ break; };
if($cell < $firstDay){
echo '<td></td>';
$cell++;
}else{
echo '<td>'.$day.'</td>';
$day++;
};
};
echo '</tr>';
};
echo '</table>';
?>
The first cells not belonging to the month are filled in blank, that works correctly, but at the end of the table there is a break. I want to fill in the remaining days of that row with a blank cell, but I cant figure it out how.
I think it has to be here, but how:
if($day > $days){ break; };
I hope the question is clear, sorry for my English!
Upvotes: 0
Views: 118
Reputation: 137
remove the code from loop
if($day > $days){ break; };
change the if condition to
if($cell < $firstDay || $day > $days){
i tested it.
Upvotes: 1