原生php写一个日历

闲来无事,写个超级简单的日历,先看效果

一、获取要展示的月份

用get或其他方法获取数据,默认为当前年月

$ny = $_GET['ny'] ? $_GET['ny'] : date('Y-m');

二、获取对应月份的总天数

将上面的日期转时间戳,然后获取对应月份的总天数

$nyt = strtotime($ny);
$days = date("t", $nyt);

三、获取第一天是周几

$xh = date("w", $nyt);

四、输出数组拼装

$data = [
    ['d' => '一', "xh" => 1],
    ['d' => '二', "xh" => 2],
    ['d' => '三', "xh" => 3],
    ['d' => '四', "xh" => 4],
    ['d' => '五', "xh" => 5],
    ['d' => '六', "xh" => 6],
    ['d' => '日', "xh" => 7],
];
if ($xh != 1) {
    for ($i = 1; $i < $xh; $i++) {
        $data[] = ['d' => '', 'xh' => $i];
    }
}
for ($i = 1; $i <= $days; $i++) {
    $data[] = ['d' => $i, 'xh' => $xh];
    $xh++;
    if ($xh > 7) {
        $xh = 1;
    }
}
if ($xh != 1) {
    for ($i = $xh; $i <= 7; $i++) {
        $data[] = ['d' => '', 'xh' => $i];
    }
}

五、输出html内容

根据自己情况修改,就是用的foreach

<table class="table table-bordered text-center">
            <?php foreach ($data as $index => $day) { ?>
                <foreach name="data" key="index" item="day">
                    <?php if ($day['xh'] == 1) echo "<tr>"; ?>
                    <td>
                        <?php echo $day['d']; ?>
                    </td>
                    <?php if ($day['xh'] == 7) echo "</tr>"; ?>
                <?php } ?>
        </table>

六、添加其他内容,比如form

<form action="rili.php" id="riliform" class="form d-flex justify-content-left mb-3">
       <input type="month"  name="ny" class="form-control" style="width: 200px;" value="<?php echo $ny; ?>">
       <input type="submit" value="查看" class="btn btn-primary">
</form>

七、完整代码

<?php
$ny = $_GET['ny'] ? $_GET['ny'] : date('Y-m');
$nyt = strtotime($ny);
$xh = date("w", $nyt);
$days = date("t", $nyt);
$data = [
    ['d' => '一', "xh" => 1],
    ['d' => '二', "xh" => 2],
    ['d' => '三', "xh" => 3],
    ['d' => '四', "xh" => 4],
    ['d' => '五', "xh" => 5],
    ['d' => '六', "xh" => 6],
    ['d' => '日', "xh" => 7],
];

if ($xh != 1) {
    for ($i = 1; $i < $xh; $i++) {
        $data[] = ['d' => '', 'xh' => $i];
    }
}
for ($i = 1; $i <= $days; $i++) {
    $data[] = ['d' => $i, 'xh' => $xh];
    $xh++;
    if ($xh > 7) {
        $xh = 1;
    }
}
if ($xh != 1) {
    for ($i = $xh; $i <= 7; $i++) {
        $data[] = ['d' => '', 'xh' => $i];
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日历</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        td {
            line-height: 5em;
        }
    </style>
</head>

<body>
    <div class="container-fluid pt-2">
        <div>
            <form action="rili.php" id="riliform" class="form d-flex justify-content-left mb-3">
                <input type="month" name="ny" class="form-control" style="width: 200px;" value="<?php echo $ny; ?>">
                <input type="submit" value="查看" class="btn btn-primary">
            </form>

        </div>
        <table class="table table-bordered text-center">
            <?php foreach ($data as $index => $day) { ?>
                <foreach name="data" key="index" item="day">
                    <?php if ($day['xh'] == 1) echo "<tr>"; ?>
                    <td>
                        <?php echo $day['d']; ?>
                    </td>
                    <?php if ($day['xh'] == 7) echo "</tr>"; ?>
                <?php } ?>
        </table>
    </div>
    <script>
        $('td').hover(function() {
            $('td').removeClass("bg-light");
            $(this).addClass("bg-light");
        })
    </script>
</body>

</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值