I'm Sham
一个在通往码农道路上走走停停的行政文员

PHP实现简易日历

因为公司准备在小程序中做一个预订餐次的功能,因为对js不是太在行,所以想着通过PHP来生成日历数据,再传给小程序使用。

以下就是PHP生成1个月的所有日期,并补全头尾2周缺的上学月日期(小程序只需要用到底部2个函数就够了),直接上代码,想看效果可以访问:

http://demo.shamqu.cn/calendar.php

以下是小程序里的效果图

<html>
    <head>
        <style>
            .items{
                width: 450px;
                margin:10px auto;
                text-align: center;
                display: flex;
                flex-direction: row;
            }
            .item{
                width: 30px;
                height:30px;
                border-radius: 50%;
                font-size:16px;
                margin:0px 10px;
                border:1px solid #ccc;
                text-align: center;
                line-height:30px;
            }
            .gray{
                color:#eee;
            }
            .bggray{
                background:#bbb;
            }
            .bgorange{
                background:#FF9833;
            }
            .last_month{
                width: 0;
                  height: 0;
                  border-right: 24px solid #555;
                  border-top: 12px solid transparent;
                  border-bottom: 12px solid transparent;
                  margin-right: 20px;
            }
            .next_month{
                width: 0;
                height: 0;
                border-left: 24px solid #555;
                border-top: 12px solid transparent;
                border-bottom: 12px solid transparent;
                margin-left: 20px;
            }
        </style>
        
    </head>
    <body>
        <?php
            $time = $_GET['day'] != '' ? strtotime($_GET['day']) : time();
            $dates = get_month_day_list($time);
            $this_m = date('m',$time);
        ?>
        <div class="items">
            <a href="?day=<?php echo date('Y-m-d',strtotime('-1 month',$time));?>"><div class="last_month"></div></a>
            <div class="current_month"><?php echo date('Y-m',$time); ?></div>
            <a href="?day=<?php echo date('Y-m-d',strtotime('+1 month',$time)); ?>"><div class="next_month"></div></a>
        </div>
        <div class="items">
            <div class="item bggray">一</div>
            <div class="item bggray">二</div>
            <div class="item bggray">三</div>
            <div class="item bggray">四</div>
            <div class="item bggray">五</div>
            <div class="item bgorange">六</div>
            <div class="item bgorange">日</div>
        </div>
        <!--循环生成每一周的行-->
        <?php
            foreach ($dates as $k => $v){
        ?>
        <div class="items">
             <!--循环生成每一周里的日期-->
            <?php
                foreach($v as $kk => $vv){
                    echo '<div class="item '.($vv['month']==$this_m ? "" : "gray").'">'.$vv['day'].'</div>';
                }
            ?>
            
        </div>
        <?php
            }
        ?>
    </body>
</html>

<?php
    //获取某个月的日期列表
    function get_month_day_list($time) {
        $days = []; 
        //首先循环获取当月日期并补全日期使其全部为完整星期
        for ($i = 1; $i <= date('t', $time); $i++) {
            $date = date('Y-m-d',strtotime('+' . $i - date('d', $time) . ' days', $time));
            $week = date('w',strtotime($date))==0 ? 7 : date('w',strtotime($date));
            if($i==1){
                //判断当月第一天的时候,前面补全缺的星期对应的日期
               for($ii=1;$ii<=$week;$ii++){
                    $days[] =  formart_days(date('Y-m-d',strtotime('+' . $ii-$week." days",strtotime($date))));
               } 
            }
            elseif($i == date('t', $time) && $week !=7){
                //判断如果当月最后第一天不是周日的时候,后面补全缺的星期对应的日期
                for($ii=0;$ii<=(7-$week);$ii++){
                    $days[] = formart_days(date('Y-m-d',strtotime('+' . $ii." days",strtotime($date))));
               } 
            }else{
                $days[] = formart_days($date);
            }
            
        }
        return array_chunk($days,7); //通过函数将数组按每周7天,分成按周分配的二维数组
    }
    
    //将日期格式化成数组
    function formart_days($date){
        return  [
                    'day' => date('d',strtotime($date)),
                    'month' => date('m',strtotime($date)),
                    'year' => date('Y',strtotime($date)),
                    'date' => date('Y-m-d',strtotime($date)),
                    'week' => date('w',strtotime($date))==0 ? 7 : date('w',strtotime($date)),
                    'week_number' => date('W',strtotime($date))+1
                ];
    } 
    
?>
赞(0) 赏杯咖啡!
未经允许不得转载:Sham@双目瞿 » PHP实现简易日历

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

如果你觉得文章好,请赏1杯速溶咖啡给Sham吧!

微信扫一扫打赏