"for" loop inside another "for" loop
$months = 12;
$monthsOfTheyear = array("Januany","February","March","April","May","June","July","August","September","October","November","December");
$currentMonth = date("n");
$currentYear = date("Y");
$daysOfTheMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
for($i = 0; $i < $months; $i++){
echo " <tbody class='month'>";
echo " <tr>
<td colspan='".$daysOfTheMonth."'>
".$monthsOfTheyear[$i]."
</td>
</tr>
<tr>
";
$daysOfEac开发者_开发知识库hMonth = cal_days_in_month(CAL_GREGORIAN, $i, $currentYear);
for($d = 1; $d <= $daysOfEachMonth; $d++){
echo " <td>
".$d."
</td>
";
}
echo " </tr>
</tbody";
}
I'm obviously doing something wrong, but I've been staring at the monitor for about an hour trying to figure it out.
I'd appreciate any advice. ThanksIn
$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i, $currentYear);
You are using $i. The first value is 0. That's why you don't get any value.
if u use
$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i+1, $currentYear);
you solve your problem
You are passing 0
as month number of January to function cal_days_in_month
which is incorrect. The function expects 1
for January.
So change $i
to $i+1
:
$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i+1, $currentYear);
^^
Looks like you've warnings disabled. If you enable them you'll see the following warning for your existing code:
PHP Warning: cal_days_in_month(): invalid date.
If this line:
$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i, $currentYear);
Becomes this line:
$daysOfEachMonth = cal_days_in_month(CAL_GREGORIAN, $i + 1, $currentYear);
Does it solve anything?
精彩评论