<?php
/**
* 万年历
* 1. 确定1号是星期几
* 2. 确定一个月有多少天
* @author Clown
* @email 812827345@qq.com
* @time 2021/8/10 16:09
*/
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$month = isset($_GET['month']) ? $_GET['month'] : date('m');
if($month > 12){
$month = 1;
++$year;
}
if($month < 1){
$year--;
$month = 12;
}
$t = date('t', strtotime($year . '-' . $month));#获取一月有多少天
$w = date('w', strtotime($year . '-' . $month . '-01'));#获取1号是星期几
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
a{text-decoration: none}
</style>
</head>
<body>
<p style="text-align: center;">
<a href="?year=<?php echo $year - 1; ?>&month=<?php echo $month;?>"><< </a>
<a href="?year=<?php echo $year; ?>&month=<?php echo $month - 1;?>">< </a>
<span style="font-size: 20px;font-weight: 700"><?php echo $year . '年' . $month . '月'; ?></span>
<a href="?year=<?php echo $year; ?>&month=<?php echo $month + 1;?>">> </a>
<a href="?year=<?php echo $year + 1; ?>&month=<?php echo $month;?>">>></a>
</p>
<?php
echo '<table align="center" border="1" width="800">';
echo <<<EOF
<tr>
<td>星期日</td>
<td>星期一</td>
<td>星期二</td>
<td>星期三</td>
<td>星期四</td>
<td>星期五</td>
<td>星期六</td>
</tr>
EOF;
$num = 1;
while($num <= $t){
echo '<tr>';
//这里$i=0的原因是date('w')获取的星期天就是0
for($i = 0; $i < 7; $i++){
//确定输出空格的条件
//当$num大于本月天数的时候输出空格 或者 确定一号的位置 当$num等于1的时候且$w大于$i的时候也输出空格
if($num > $t || ($w > $i && $num == 1)){
echo '<td> </td>';
}else{
echo '<td>' . $num . '</td>';
$num++;
}
}
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>
 
|