用php寫一個簡單低配版的萬年曆

//當前年
$year=$_GET['y']?$_GET['y']:date('Y');
//當前月
$month=$_GET['m']?$_GET['m']:date('m');
//當前月1號的時間戳
$firstday=strtotime("{$year}-{$month}-1");
//當前月天數
$days=date('t',$firstday);
//當前月1號是周幾
$week=date('w',$firstday);
// echo "$week";

// 上一年和上一月
$prevyear=$year;
$prevmonth=$month-1;
if($prevmonth<1){
    $prevyear=$year - 1;
    $prevmonth=12;
}

// 下一年和下一月
$nextyear=$year;
$nextmonth=$month+1;
if($nextmonth>12){
    $nextyear=$year + 1;
    $nextmonth=1;
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            font-family:微軟雅黑;
        }
        a{
            text-decoration:none;
            color:#55f;
        }
    </style>
</head>
<body>
    <center>
       <h2>萬年曆-<?php echo $year ?>年<?php echo $month ?>月</h2>
       <table border="1px" cellspacing="0" width="700px">
           <thead>
               <tr>
                   <th>週日</th>
                   <th>週一</th>
                   <th>週二</th>
                   <th>週三</th>
                   <th>週四</th>
                   <th>週五</th>
                   <th>週六</th>
               </tr>
           </thead>
             <?php
             
             for($i=(1-$week);$i<=$days;){
                 echo '<tr>';
                 for($j=0;$j<7;$j++,$i++){
                     if($i>$days || $i<1){
                         echo "<td>&nbsp; </td>";
                     }else{
                         echo "<td>{$i}</td>";
                     }
                     
                 }
                 echo '</tr>';
             }
             ?>
       </table>
       <h3>
           <a href="new-datetable.php?y=<?php echo $prevyear ?>&m=<?php echo $prevmonth ?>">上一月</a>|
           <a href="new-datetable.php?y=<?php echo $nextyear ?>&m=<?php echo $nextmonth ?>">下一月</a>
       </h3>
    </center>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章