PHP小记——万年历
css样式
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
table{
width: 200px;
height: 190px;
border: 1px solid gray;
float: left;
margin-left: 24px;
margin-bottom: 20px;
}
tr,td{
height: 20px;
}
.context{
width: 1349px;
}
.top{
text-align: center;
margin-top: 40px;
margin-bottom: 40px;
font-size: 30px;
height: 40px;
}
</style>
php代码
<body>
<?php
//设置东八区
date_default_timezone_get('Asia/Shanghai');
$year = 0;
if(isset($_GET['year'])){
$year = $_GET['year'];
}
if (isset($_GET['lastyear'])) {
if (isset($_GET['year'])) {
$year = $_GET['year']-1;
}
}
if (isset($_GET['foryear'])) {
if (isset($_GET['year'])) {
$year = $_GET['year']+1;
}
}
?>
<div class="top">
<form>
<input type="submit" name="lastyear" value="<<上一年">
<input type="texy" name="year" placeholder="请输入年份" value="<?php echo $year;?>">
<input type="submit" name="foryear" value="下一年>>">
<br>
<input type="submit" name="check" value="查询">
</form>
</div>
<div class="context">
<?php
if (isset($_GET['check'])) {
if(isset($_GET['year'])){
$year = $_GET['year'];
}
show($year);
}
if (isset($_GET['lastyear'])) {
if (isset($_GET['year'])) {
$year = $_GET['year']-1;
}
show($year);
}
if (isset($_GET['foryear'])) {
if (isset($_GET['year'])) {
$year = $_GET['year']+1;
}
show($year);
}
function show($year){
for ($i = 1; $i <= 12 ; $i++) {
echo "<table>";
echo "<tr><th colspan='7'>".$i."</th></tr>";
echo "<tr><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td><td>日</td></tr>";
echo "<tr>";
$date_num = 1;
$max_date_num = 31;//31代表当前月的天数
//数字转变,例如1转为01
$month = sprintf("%02d",$i);
//获取当前月的起始日期
$begin_date = $year.$month."01";
//获取起始日期是星期几
$begin = (int)date('w',strtotime($begin_date));
if ($i==1||$i==3||$i==5||$i==7||$i==7||$i==8||$i==10||$i==12) {
$max_date_num = 31;
}elseif ($i==2) {
if($year%4==0){
$max_date_num = 29;
}else{
$max_date_num = 28;
}
}else{
$max_date_num = 30;
}
if ($begin==0) {
$begin = 7;
}
for ($j=1; $j <=7 ; $j++) {
if ($j<$begin) {//3代表本月从星期三开始,这个值是需要动态的根据每个月的起始不同而变化
echo "<td>"."</td>";
}else{
echo"<td>".$date_num."</td>";
$date_num += 1;
}
}
while ($date_num <= $max_date_num) {
echo "</tr>";
echo "<tr>";
for ($j=1; $j <=7 ; $j++) {
echo"<td>".$date_num."</td>";
$date_num += 1;
if ($date_num > $max_date_num) {
break;
}
}
echo "</tr>";
}
echo "</table>";
}
}
?>
</div>
</body>