<?php
$time=time();
//计算当前年
$year=$_GET['y']?$_GET['y']:date('Y',$time);
//计算当前月
$month=$_GET['m']?$_GET['m']:date('m',$time);
//当前月总计多少天?
$days=date('t',strtotime("{$year}-{$month}-1"));
//当前月的第一天是周几?
$week=date('w',strtotime("{$year}-{$month}-1"));
//计算万年历格中第一天的数字
$first=1-$week;
//上一月和上一年
$prevMonth=$month-1;
$prevYear=$year;
if($prevMonth<1){
$prevMonth=12;
$prevYear=$year-1;
}
//下一月和下一年
$nextMonth=$month+1;
$nextYear=$year;
if($nextMonth>12){
$nextMonth=1;
$nextYear=$year+1;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
a{
text-decoration: none;
color:#00f;
}
</style>
</head>
<body>
<center>
<?php
echo "<h2>万年历-{$year}年{$month}月</h2>";
?>
<table width='700px' border='1px' cellspacing='0'>
<tr>
<th>周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
</tr>
<?php
//布局万年历的表格
for($i=$first;$i<=$days;){
echo '<tr>';
for($j=0;$j<7;$j++){
if($i<=$days && $i>=1){
echo "<td>{$i}</td>";
}else{
echo '<td> </td>';
}
$i++;
}
echo '</tr>';
}
?>
</table>
<h3>
<a href="index.php?y=<?php echo $prevYear?>&m=<?php echo $prevMonth ?>">上一月</a> |
<a href="index.php?y=<?php echo $nextYear?>&m=<?php echo $nextMonth ?>">下一月</a>
</h3>
</center>
</body>
</html>