当前位置:Gxlcms > PHP教程 > 教大家制作简单的php日历

教大家制作简单的php日历

时间:2021-07-01 10:21:17 帮助过:9人阅读

最近的一个项目中,需要将数据用日历方式显示,网上有很多的JS插件,后面为了自己能有更大的控制权,决定自己制作一个日历显示。如下图所示:

一、计算数据
1、new一个Calendar类

2、初始化两个下拉框中的数据,年份与月份

3、初始化要搜索的年份和月份

4、计算得出日历中每一天的数据信息,包括css、天数

  1. <?php
  2. require_once 'calendar.php';
  3. $util = new Calendar();
  4. $years = array(2012, 2013, 2014, 2015, 2016);//年份选择自定义
  5. $months = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//月份数组
  6. //获取post的年份数据
  7. if(empty($_POST['ddlYear'])) {
  8. $year = date('Y');
  9. }else {
  10. $year = $_POST['ddlYear'];
  11. }
  12. //获取post的月份数据
  13. if(empty($_POST['ddlMonth'])) {
  14. $month = date('n');
  15. }else {
  16. $month = $_POST['ddlMonth'];
  17. }
  18. $calendar = $util->threshold($year, $month);//获取各个边界值
  19. $caculate = $util->caculate($calendar);//计算日历的天数与样式
  20. $draws = $util->draw($caculate);//画表格,设置table中的tr与td
  21. ?>

二、html展示
1、休息天的背景色是不同的,不是当前搜索年月的天数字体颜色也是不同的

2、div中做初始化年份与月份的下拉框的操作,并选中当前要搜索的年月

3、数据已计算好,哪个td属于哪个tr也已做好,直接将table打印出来即可
  

  1. <div style="padding:20px">
  2. <select name="ddlYear">
  3. <?php foreach($years as $data) {?>
  4. <option value="<?php echo $data?>" <?php if($year == $data) echo 'selected="selected"'?>><?php echo $data?></option>
  5. <?php }?>
  6. </select>
  7. <select name="ddlMonth">
  8. <?php foreach($months as $data) {?>
  9. <option value="<?php echo $data?>" <?php if($month == $data) echo 'selected="selected"'?>><?php echo $data?></option>
  10. <?php }?>
  11. </select>
  12. <input type="submit" value="修改"/>
  13. </div>
  14. <table width="100%" cellspacing="0" class="table_calendar">
  15. <thead class="f14">
  16. <tr>
  17. <td width="16%">日</td>
  18. <td width="14%">一</td>
  19. <td width="14%">二</td>
  20. <td width="14%">三</td>
  21. <td width="14%">四</td>
  22. <td width="14%">五</td>
  23. <td width="14%">六</td>
  24. </tr>
  25. </thead>
  26. <tbody class="f14">
  27. <?php foreach($draws as $draw) {?>
  28. <tr>
  29. <?php foreach($draw as $date) {?>
  30. <td class="<?php echo $date['tdclass']?>">
  31. <p class="<?php echo $date['pclass']?>"><?php echo $date['day']?></p>
  32. </td>
  33. <?php }?>
  34. </tr>
  35. <?php }?>
  36. </tbody>
  37. </table>

三、Calendar类
1、threshold方法,生成日历的各个边界值

  1)计算这个月总天数

  2)计算这个月第一天与最后一天,各是星期几

  3)计算日历中的第一个日期与最后一个日期
  

  1. /**
  2. * @deprecated 生成日历的各个边界值
  3. * @param string $year
  4. * @param string $month
  5. * @return array
  6. */
  7. function threshold($year, $month) {
  8. $firstDay = mktime(0, 0, 0, $month, 1, $year);
  9. $lastDay = strtotime('+1 month -1 day', $firstDay);
  10. //取得天数
  11. $days = date("t", $firstDay);
  12. //取得第一天是星期几
  13. $firstDayOfWeek = date("N", $firstDay);
  14. //获得最后一天是星期几
  15. $lastDayOfWeek = date('N', $lastDay);
  16. //上一个月最后一天
  17. $lastMonthDate = strtotime('-1 day', $firstDay);
  18. $lastMonthOfLastDay = date('d', $lastMonthDate);
  19. //下一个月第一天
  20. $nextMonthDate = strtotime('+1 day', $lastDay);
  21. $nextMonthOfFirstDay = strtotime('+1 day', $lastDay);
  22. //日历的第一个日期
  23. if($firstDayOfWeek == 7)
  24. $firstDate = $firstDay;
  25. else
  26. $firstDate = strtotime('-'. $firstDayOfWeek .' day', $firstDay);
  27. //日历的最后一个日期
  28. if($lastDayOfWeek == 6)
  29. $lastDate = $lastDay;
  30. elseif($lastDayOfWeek == 7)
  31. $lastDate = strtotime('+6 day', $lastDay);
  32. else
  33. $lastDate = strtotime('+'.(6-$lastDayOfWeek).' day', $lastDay);
  34. return array(
  35. 'days' => $days,
  36. 'firstDayOfWeek' => $firstDayOfWeek,
  37. 'lastDayOfWeek' => $lastDayOfWeek,
  38. 'lastMonthOfLastDay' => $lastMonthOfLastDay,
  39. 'firstDate' => $firstDate,
  40. 'lastDate' => $lastDate,
  41. 'year' => $year,
  42. 'month' => $month
  43. );
  44. }

2、caculate方法,计算日历的天数与样式

  1)将上个月的天数计算出来,本月第一天的星期不是星期天的话,就需要根据上个月的最后一天计算

  2)将本月的天数遍历出来,如果是休息天就加上特殊的css样式

  3)将下个月的天数计算出来,分三种情况,星期日、星期六和工作日
  

  1. /**
  2. * @author Pwstrick
  3.    * @param array $calendar 通过threshold方法计算后的数据
  4. * @deprecated 计算日历的天数与样式
  5. */
  6. function caculate($calendar) {
  7. $days = $calendar['days'];
  8. $firstDayOfWeek = $calendar['firstDayOfWeek'];//本月第一天的星期
  9. $lastDayOfWeek = $calendar['lastDayOfWeek'];//本月最后一天的星期
  10. $lastMonthOfLastDay = $calendar['lastMonthOfLastDay'];//上个月的最后一天
  11. $year = $calendar['year'];
  12. $month = $calendar['month'];
  13. $dates = array();
  14. if($firstDayOfWeek != 7) {
  15. $lastDays = array();
  16. $current = $lastMonthOfLastDay;//上个月的最后一天
  17. for ($i = 0; $i < $firstDayOfWeek; $i++) {
  18. array_push($lastDays, $current);//添加上一个月的日期天数
  19. $current--;
  20. }
  21. $lastDays = array_reverse($lastDays);//反序
  22. foreach ($lastDays as $index => $day) {
  23. array_push($dates, array('day' => $day, 'tdclass' => ($index ==0 ?'rest':''), 'pclass' => 'outter'));
  24. }
  25. }
  26. //本月日历信息
  27. for ($i = 1; $i <= $days; $i++) {
  28. $isRest = $this->_checkIsRest($year, $month, $i);
  29. //判断是否是休息天
  30. array_push($dates, array('day' => $i, 'tdclass' => ($isRest ?'rest':''), 'pclass' => ''));
  31. }
  32. //下月日历信息
  33. if($lastDayOfWeek == 7) {//最后一天是星期日
  34. $length = 6;
  35. }
  36. elseif($lastDayOfWeek == 6) {//最后一天是星期六
  37. $length = 0;
  38. }else {
  39. $length = 6 - $lastDayOfWeek;
  40. }
  41. for ($i = 1; $i <= $length; $i++) {
  42. array_push($dates, array('day' => $i, 'tdclass' => ($i==$length ?'rest':''), 'pclass' => 'outter'));
  43. }
  44. return $dates;
  45. }

3、draw方法,画表格,设置table中的tr与td

  1)数据将要用table标签来显示,所以这里要将各个tr下面的td排列好

  2)$index % 7 == 0 计算表格每行的第一列

  3)$index % 7 == 6 || $index == ($length-1) 计算每行的最后一列,或$caculate的最后一个数据

  4)将中间行添加到$tr中,就是每一行的array

  1.  /**
  2. * @author Pwstrick
  3. * @param array $caculate 通过caculate方法计算后的数据
  4. * @deprecated 画表格,设置table中的tr与td
  5. */
  6. function draw($caculate) {
  7. $tr = array();
  8. $length = count($caculate);
  9. $result = array();
  10. foreach ($caculate as $index => $date) {
  11. if($index % 7 == 0) {//第一列
  12. $tr = array($date);
  13. }elseif($index % 7 == 6 || $index == ($length-1)) {
  14. array_push($tr, $date);
  15. array_push($result, $tr);//添加到返回的数据中
  16. $tr = array();//清空数组列表
  17. }else {
  18. array_push($tr, $date);
  19. }
  20. }
  21. return $result;
  22. }

通过本文大家应该知道日历制作的方法了,那就趁热打铁,做一个属于自己日历。

附源码:教大家制作简单的php日历

人气教程排行