PHP万年历

1 篇文章 0 订阅
1 篇文章 0 订阅
<?php
/**
 * 万年历
 * 1. 确定1号是星期几
 * 2. 确定一个月有多少天
 * @author Clown
 * @email 812827345@qq.com
 * @time 2021/8/10   16:09
 */

    $year = isset($_GET['year']) ? $_GET['year'] : date('Y');
    $month = isset($_GET['month']) ? $_GET['month'] : date('m');
    if($month > 12){
        $month = 1;
        ++$year;
    }
    if($month < 1){
        $year--;
        $month = 12;
    }
    $t = date('t', strtotime($year . '-' . $month));#获取一月有多少天
    $w = date('w', strtotime($year . '-' . $month . '-01'));#获取1号是星期几

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        a{text-decoration: none}
    </style>
</head>
<body>
    <p style="text-align: center;">
        <a href="?year=<?php echo $year - 1; ?>&month=<?php echo $month;?>"><<&nbsp;&nbsp;</a>
        <a href="?year=<?php echo $year; ?>&month=<?php echo $month - 1;?>"><&nbsp;&nbsp;</a>
        <span style="font-size: 20px;font-weight: 700"><?php echo $year . '年' . $month . '月'; ?></span>
        <a href="?year=<?php echo $year; ?>&month=<?php echo $month + 1;?>">>&nbsp;&nbsp;</a>
        <a href="?year=<?php echo $year + 1; ?>&month=<?php echo $month;?>">>></a>
    </p>
    <?php
        echo '<table align="center" border="1" width="800">';
        echo <<<EOF
            <tr>
            <td>星期日</td>
            <td>星期一</td>
            <td>星期二</td>
            <td>星期三</td>
            <td>星期四</td>
            <td>星期五</td>
            <td>星期六</td>
        </tr>
EOF;
        $num = 1;
        while($num <= $t){
            echo '<tr>';
            //这里$i=0的原因是date('w')获取的星期天就是0
            for($i = 0; $i < 7; $i++){
                //确定输出空格的条件
                //当$num大于本月天数的时候输出空格 或者 确定一号的位置 当$num等于1的时候且$w大于$i的时候也输出空格
                if($num > $t || ($w > $i && $num == 1)){
                    echo '<td>&nbsp;</td>';
                }else{
                    echo '<td>' . $num . '</td>';
                    $num++;
                }

            }
            echo '</tr>';
        }
        echo '</table>';
    ?>
</body>
</html>



在这里插入图片描述
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值