侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

教大家制作简单的php日历

php  /  管理员 发布于 7年前   161

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

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

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

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

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

threshold($year, $month);//获取各个边界值 $caculate = $util->caculate($calendar);//计算日历的天数与样式 $draws = $util->draw($caculate);//画表格,设置table中的tr与td?>

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

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

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

日 一 二 三 四 五 六

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

  1)计算这个月总天数

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

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

/**  * @deprecated 生成日历的各个边界值  * @param string $year  * @param string $month  * @return array  */ function threshold($year, $month) {  $firstDay = mktime(0, 0, 0, $month, 1, $year);  $lastDay = strtotime('+1 month -1 day', $firstDay);  //取得天数   $days = date("t", $firstDay);  //取得第一天是星期几  $firstDayOfWeek = date("N", $firstDay);  //获得最后一天是星期几  $lastDayOfWeek = date('N', $lastDay);    //上一个月最后一天  $lastMonthDate = strtotime('-1 day', $firstDay);  $lastMonthOfLastDay = date('d', $lastMonthDate);  //下一个月第一天  $nextMonthDate = strtotime('+1 day', $lastDay);  $nextMonthOfFirstDay = strtotime('+1 day', $lastDay);    //日历的第一个日期  if($firstDayOfWeek == 7)   $firstDate = $firstDay;  else    $firstDate = strtotime('-'. $firstDayOfWeek .' day', $firstDay);  //日历的最后一个日期  if($lastDayOfWeek == 6)   $lastDate = $lastDay;  elseif($lastDayOfWeek == 7)    $lastDate = strtotime('+6 day', $lastDay);  else   $lastDate = strtotime('+'.(6-$lastDayOfWeek).' day', $lastDay);    return array(    'days' => $days,     'firstDayOfWeek' => $firstDayOfWeek,     'lastDayOfWeek' => $lastDayOfWeek,    'lastMonthOfLastDay' => $lastMonthOfLastDay,    'firstDate' => $firstDate,    'lastDate' => $lastDate,    'year' => $year,    'month' => $month  ); }

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

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

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

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

/**  * @author Pwstrick   * @param array $calendar 通过threshold方法计算后的数据  * @deprecated 计算日历的天数与样式  */ function caculate($calendar) {  $days = $calendar['days'];  $firstDayOfWeek = $calendar['firstDayOfWeek'];//本月第一天的星期  $lastDayOfWeek = $calendar['lastDayOfWeek'];//本月最后一天的星期  $lastMonthOfLastDay = $calendar['lastMonthOfLastDay'];//上个月的最后一天  $year = $calendar['year'];  $month = $calendar['month'];    $dates = array();  if($firstDayOfWeek != 7) {   $lastDays = array();   $current = $lastMonthOfLastDay;//上个月的最后一天   for ($i = 0; $i < $firstDayOfWeek; $i++) {    array_push($lastDays, $current);//添加上一个月的日期天数    $current--;   }   $lastDays = array_reverse($lastDays);//反序   foreach ($lastDays as $index => $day) {    array_push($dates, array('day' => $day, 'tdclass' => ($index ==0 ?'rest':''), 'pclass' => 'outter'));   }  }    //本月日历信息  for ($i = 1; $i <= $days; $i++) {   $isRest = $this->_checkIsRest($year, $month, $i);   //判断是否是休息天   array_push($dates, array('day' => $i, 'tdclass' => ($isRest ?'rest':''), 'pclass' => ''));  }    //下月日历信息  if($lastDayOfWeek == 7) {//最后一天是星期日   $length = 6;  }  elseif($lastDayOfWeek == 6) {//最后一天是星期六   $length = 0;  }else {   $length = 6 - $lastDayOfWeek;  }  for ($i = 1; $i <= $length; $i++) {   array_push($dates, array('day' => $i, 'tdclass' => ($i==$length ?'rest':''), 'pclass' => 'outter'));  }    return $dates; }

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

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

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

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

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

 /**  * @author Pwstrick  * @param array $caculate 通过caculate方法计算后的数据  * @deprecated 画表格,设置table中的tr与td  */ function draw($caculate) {  $tr = array();  $length = count($caculate);  $result = array();  foreach ($caculate as $index => $date) {   if($index % 7 == 0) {//第一列    $tr = array($date);   }elseif($index % 7 == 6 || $index == ($length-1)) {    array_push($tr, $date);    array_push($result, $tr);//添加到返回的数据中    $tr = array();//清空数组列表   }else {    array_push($tr, $date);   }  }  return $result; }

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

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

您可能感兴趣的文章:

  • php+javascript的日历控件
  • 用 php 编写的日历
  • php下实现农历日历的代码
  • php+mysql+jquery实现日历签到功能
  • PHP完整的日历类(CLASS)
  • PHP输出日历表代码实例
  • PHP实现的简单日历类
  • 基于ThinkPHP实现的日历功能实例详解
  • php实现的日历程序
  • 分享3个php获取日历的函数
  • PHP实现的日历功能示例


  • 上一条:
    超详细的php用户注册页面填写信息完整实例(附源码)
    下一条:
    PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客