<?php
function linha($semana){
echo "<tr>";
for ($i=0; $i<=6; $i++):
if(isset($semana[$i])):
echo "<td>$semana[$i]</td>";
else:
echo "<td> </td>";
endif;
endfor;
echo "</tr>";
}
function calendario(){
$dia = 1;
$semana = array();
while ($dia <= 31):
array_push($semana,$dia);
if(count($semana)== 7) :
linha($semana);
$semana = array();
endif;
$dia++;
endwhile;
linha($semana);
}
?>
<table border="1">
<tr>
<th>Dom</th>
<th>Seg</th>
<th>Ter</th>
<th>Qua</th>
<th>Quin</th>
<th>Sex</th>
<th>Sáb</th>
</tr>
<?php calendario() ?>
</table>