<table>
<caption>
<?=date('Y年m月')?>
</caption>
<thead>
<tr>
<th scope="col" title="星期一">一</th>
<th scope="col" title="星期二">二</th>
<th scope="col" title="星期三">三</th>
<th scope="col" title="星期四">四</th>
<th scope="col" title="星期五">五</th>
<th scope="col" title="星期六">六</th>
<th scope="col" title="星期日">日</th>
</tr>
</thead>
<!-- <tfoot>
<tr>
<td colspan="3" id="prev"><a href="/blog/888?m=201210" title="查看 2012 年十月的文章">« 十</a></td>
<td class="pad"> </td>
<td colspan="3" id="next" class="pad"> </td>
</tr>
</tfoot> -->
<tbody>
<?php
$stamp = strtotime('-'.date('j').' days'); //月第一天的时间戳
$total_days = date('t',$stamp);//月共有天数
$week_day = date('N',$stamp);//周几1-7
$calendar = '';
for($i=1; $i<$total_days+$week_day; $i++){
$calendar .= ($i==1) ? '<tr>' : '';
if($i<=$week_day){
$calendar .= '<td></td>';
}else{
$stamp = strtotime('next day',$stamp);
$today = (date('Ymd',$stamp) == date('Ymd')) ? 'id="today"' : '';
$calendar .= '<td '.$today.'>'.date('d',$stamp).'</td>';
}
$calendar .= ($i%7==0) ? '</tr><tr>' : '';
$calendar .= ($i+1 == $total_days+$week_day) ? str_repeat('<td></td>', 7-date('N',$stamp)).'</tr>' : '';
}
echo $calendar;
?>
</tbody>
</table>