PHP calendar implementation case

墨辰丷
Release: 2023-03-25 22:30:02
Original
6265 people have browsed it

This article mainly introduces a small case of how to write a calendar in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The code is as follows:

<?php
    //修改页面编码
    header("Content-type: text/html; charset=utf-8");
    //如果没有传入年份则获取当前系统年份
    $year=!isset($_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"));
    //当前1号是星期几
    $week=date(&#39;w&#39;,strtotime("{$year}-{$month}-1"));

    //输出表头
    echo "<center>";
        echo "<h2>{$year}年{$month}月</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<=0){
                    echo "<td> </td>";
                }else{
                echo "<td>{$i}</td>";
                }
                $i++;
            }
            echo "</tr>";
        }

        echo "</table>";

        //实现上一月和上一年
        if($month==1){
            $premonth = 12;
            $preyear = $year - 1;
        }else{
            $premonth = $month-1;
            $preyear = $year;
        }

        //实现下一月和下一年
        if($month==12){
            $nextmonth = 1;
            $nextyear = $year + 1;
        }else{
            $nextmonth = $month + 1;
            $nextyear = $year;
        }
        //上一月、下一月的实现
        echo "<a href=&#39;http://localhost/index.php?y={$premonth}&m={$premonth}&#39;>上个月</a>";
        echo "<a href=&#39;http://localhost/index.php?y={$nextmonth}&m={$nextmonth}&#39;>下个月</a>";

    echo "</center>";
?>
Copy after login

Related recommendations:

October 10, 2013 Almanac A PHP Calendar Program

phpcalendarcode Share simple and practical php calendar code

phpcalendarcodeefficient calendar code implemented by php

The above is the detailed content of PHP calendar implementation case. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template