php+html实现简单日历功能
日历这个代码是好久之前接的项目需要用的的时候写的,之前出去面试也碰巧遇到面试官问了这个问题(不知道是不是觉得很多人不会才问的,哈哈),今天有点空就把代码从框架里面抽出来,从新写个简单点的,废话不多说,直接贴代码(代码是直接前后端混写的,分离的话html中的部分代码实际是可以直接归类到php代码处理的)
下面是PHP部分的:
<?Php $weeksList = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; $year = isset($_GET["year"]) ? $_GET["year"] : date("Y"); $month = isset($_GET["month"]) ? $_GET["month"] : date("m"); $start_weekday = date("w", mktime(0, 0, 0, $month, 1, $year));//每月开始是周几 $begin_date = date('Y-m-01', mktime(0, 0, 0, $month, 1, $year));//当前月份日期 $end_weekday = date('w', strtotime("$begin_date +1 month -1 day"));//每月结束是周几 $days = date("t", mktime(0, 0, 0, $month, 1, $year));//本月有几号 $curr_day = date("d",time()); $months = $year . "-" . $month;//当前年月 $last_year = date("Y", strtotime("last month", strtotime($months)));//当前月的上一个月的年份 $last_month = date("m", strtotime("last month", strtotime($months)));//当前月的上一个月的月份 $first_year = date("Y", strtotime("+1 month", strtotime($months)));//当前月的下一个月的年份 $first_month = date("m", strtotime("+1 month", strtotime($months)));//当前月的下一个月的月份 function get_week($date){ $start_weekday = date("w",strtotime($date)); $weeksList = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']; return $weeksList[$start_weekday]; } function get_last_day($n){ global $months; return (int)date("d",strtotime($months . "-01"."-{$n} day")); } function get_first_day($n){ global $months,$days; return (int)date("d",strtotime($months . "-" . $days."{$n} day")); } ?>
下面是html代码:
<div class="con_right"> <div class="calculator_title"> <div class="calculator_title_btn"> <a href="./test.php?<?php echo http_build_query(['year'=>$last_year,'month'=>$last_month])?>" class="calculator_title_btn_a" style="float: right;">上一个月</a> </div> <div class="calculator_title_text"> <?php echo $year;?>年<?php echo $month;?>月 </div> <div class="calculator_title_btn"> <a href="./test.php?<?php echo http_build_query(['year'=>$first_year,'month'=>$first_month])?>" class="calculator_title_btn_a" style="float: left;">下一个月</a> </div> </div> <div class="calculator_detail"> <div class="calculator_week pc"> <?php foreach ($weeksList as $k => $vo){ ?> <div class="calculator_week_span <?php if($k >= $start_weekday){ echo "week";}?>"><? echo $vo;?></div> <?php }?> </div> <div class="calculator_main"> <!-- 这里是处理当月前段日期的--> <? for($i = 0; $i < $start_weekday; $i++){?> <div class="calculator_li other_month"> <div class="calculator_week_li"><? echo $weeksList[$i];?></div> <span class="dates"><? echo get_last_day($start_weekday-$i+1);?></span><br/> <span class="month"><? echo (int)$last_month . "月";?></span> </div> <? } ?> <!-- 这里是处理当月日期的--> <? for($i = 0; $i < $days; $i++){?> <div class="calculator_li" <? if($curr_day == ($i+1)){echo "style='background:#2D5F9B;color:#ffffff;'";} ?>> <div class="calculator_week_li week"><? echo get_week($year.'-'.$month.'-'.($i+1)); ?></div> <span class="dates"><? echo $i+1;?></span><br/> <span class="month"<? if($curr_day == ($i+1)){echo "style='color:#ffffff;'";} ?>><? echo (int)$month . "月";?></span> </div> <? } ?> <!-- 这里是处理当月后段日期的--> <? $k = 1; for($i = $end_weekday; $i < 6; $i++){?> <div class="calculator_li other_month"> <div class="calculator_week_li"><? echo $weeksList[$i];?></div> <span class="dates"><? echo get_first_day($k); $k++;?></span><br/> <span class="month"><? echo (int)$first_month . "月";?></span> </div> <? } ?> </div> </div> </div>
最终效果:
然后css代码由于是剥离出来的,比较杂,就不贴出来了,下面是代码下载地址
版权声明:本文由“憨小猪”发布,如需转载请注明出处。