PHP实现万年历

PHP实现简单的万年历(实例)

<?php
    //修改页面编码
    header("Content-type: text/html; charset=utf-8");
    //如果没有传入年份则获取当前系统年份
    $year=!isset($_GET['y'])?$_GET['y']:date('Y');
    //如果没有传入月份则获取当前系统月份
    $month=$_GET['m']?$_GET['m']:date('m');

    //获取当前月有多少天
    $days=date('t',strtotime("{$year}-{$month}-1"));
    //当前1号是星期几
    $week=date('w',strtotime("{$year}-{$month}-1"));

    //输出表头
    echo "<center>";
        echo "<h2>{$year}年{$month}月</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<=0){
                    echo "<td>&nbsp;</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='http://localhost/index.php?y={$premonth}&m={$premonth}'>上个月</a>";
        echo "<a href='http://localhost/index.php?y={$nextmonth}&m={$nextmonth}'>下个月</a>";

    echo "</center>";
?>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

1、启动运行环境,在这里我使用的是phpStudy. 
2、在浏览器输入url,比如http://localhost/day.php?y=2016&m=6 
3、测试结果: 
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值