Home > Article > Backend Development > Complete code to implement perpetual calendar in php
This article brings you the complete code for implementing the perpetual calendar in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
<?php //修改页面编码 header("content-type:text/html;charset=utf-8"); //获取当前年 $year=$_GET['y']?$_GET['y']:date('Y'); //获取当年月 $month=$_GET['m']?$_GET['m']:date('m'); //获取当前月多少天 $days=date('t',strtotime("{$year}-{$month}-1")); //当前一号周几 $week=date('w',strtotime("{$year}-{$month}-1")); //居中 echo "<center>"; //计算上个月 if($month==1) { $prevyear=$year-1; $prevmonth=12; } else { $prevyear=$year; $prevmonth=$month-1; } //计算下个月 if($month==12) { $nextyear=$year+1; $nextmonth=1; } else { $nextyear=$year; $nextmonth=$month+1; } //输出表头 echo " <h2><a href='万年历.php?y={$prevyear}&m={$prevmonth}'>上一月</a>|{$year}年{$month}月|<a href='万年历.php?y={$nextyear}&m={$nextmonth}'>下一月</a></h2>"; //输出日期表格 echo "<table width='700px' border='1px'>"; echo "<tr>"; echo "<th>周日</th>"; echo "<th>周一</th>"; echo "<th>周二</th>"; echo "<th>周三</th>"; echo "<th>周四</th>"; echo "<th>周五</th>"; echo "<th>周六</th>"; echo "</tr>"; //铺表格 for ($i=1-$week; $i <=$days ;) { echo "<tr>"; for ($j=0; $j < 7; $j++) { if ($i>$days || $i<1) { echo "<td> </td>"; } else { echo "<td>{$i}</td>"; } $i++; } echo "</tr>"; } echo "</table>"; echo "</center>"; ?>
Related recommendations:
A brief introduction to abstract methods, abstract classes and interfaces in PHP
PHP wants to implement page jumps How does the function work? (Example of function tags)
The process of setting up the LNMP environment with source code (details)
The above is the detailed content of Complete code to implement perpetual calendar in php. For more information, please follow other related articles on the PHP Chinese website!