Home  >  Article  >  Backend Development  >  Complete code to implement perpetual calendar in php

Complete code to implement perpetual calendar in php

不言
不言Original
2018-08-11 09:39:2711809browse

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[&#39;y&#39;]?$_GET[&#39;y&#39;]:date(&#39;Y&#39;);

//获取当年月
$month=$_GET[&#39;m&#39;]?$_GET[&#39;m&#39;]:date(&#39;m&#39;);

//获取当前月多少天
$days=date(&#39;t&#39;,strtotime("{$year}-{$month}-1"));

//当前一号周几
$week=date(&#39;w&#39;,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=&#39;万年历.php?y={$prevyear}&m={$prevmonth}&#39;>上一月</a>|{$year}年{$month}月|<a  href=&#39;万年历.php?y={$nextyear}&m={$nextmonth}&#39;>下一月</a></h2>";

//输出日期表格
echo "<table width=&#39;700px&#39; border=&#39;1px&#39;>";
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn