PHP制作简单的日历

在这里分享一个PHP制作的日历

<?php

//万年历
if($_GET['year']){
$year = $_GET['year'];
}else{
$year = date("Y");
}  //年

if($_GET['month']){
$month = $_GET['month'];
}else{
$month = date("m");
}  //月

//计算该月有多少钱
$day = date("t",mktime(0,0,0,$month,1,$year));
//计算1号是周几
$w = date("w",mktime(0,0,0,$month,1,$year));

?>

<html>
<head>
<title>clander</title>
</head>
<body>

<center>
<h1><?php echo $year.'年'.$month.'月';?></h1>
<table width="600px" border="1px">  
<tr>
    <th style='color:#ff0000'>星期日</th>
    <th>星期一</th>
    <th>星期二</th>
    <th>星期三</th>
    <th>星期四</th>
    <th>星期五</th>
    <th style='color:#008'>星期六</th>
</tr>


<?php
 $dd = 1;
 while($dd<=$day){
   echo '<tr>';
   for($i=0;$i<7;$i++){
   if($dd<=$day && ($w<=$i ||$dd!=1)){
    echo "<td>{$dd}</td>";
    $dd++;
   }else{
    echo '<td>&nbsp</td>';
   }

  }
   echo '</tr>';
 }
//处理年份越界的问题
$prey = $nexty = $year;
$prem = $nextm = $month;
 if($prem<=1){
 $prem = 12;
 $prey--;
 }else{
 $prem--;
 }
 
 if($nextm>=12){
 $nextm = 1;
 $nexty++;
 }else{
 $nextm++;
 }
?>
<a href="clander.php?year=<?php echo $prey ?>&month=<?php echo $prem ?>">上一月</a>
<a href="clander.php?year=<?php echo $nexty ?>&month=<?php echo $nextm?>">下一月</a>

</table>
</center>
</body>
</html>

参考图:

原文地址:https://www.cnblogs.com/sunxun/p/3757912.html