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

基于ThinkPHP实现的日历功能实例详解

ThinkPHP  /  管理员 发布于 8年前   193

本文实例讲述了基于ThinkPHP实现的日历功能。分享给大家供大家参考,具体如下:

开发环境介绍

最新,闲来没事,便开发了一款简单的日历,来统计工作情况。为了开发便捷,使用ThinkPHP架构。界面如下图

备注:每页包含上一个月,当前月,下一个月的日期,并用不同的颜色区分,如果某天工作了,便圈出来。
主要是以下两个文件

重要文件描述

功能文件

CalenDar.class.php主要负责,获取日历详细信息的,不涉及用户数据操作。

代码如下:

curYear=$_get['yeal'];    }else{      $this->curYear=date('Y');    }    if(isset($_get['month']) && is_numeric($_get['month'])){      $this->curMonth=$_get['month'];    }else{      $this->curMonth=date('n');    }    if(isset($_get['day']) && is_numeric($_get['day'])){      $this->curDay=$_get['day'];    }else{      $this->curDay=date('j');    }    $this->init($dateTime);    $this->createCalendar();  }  /**  *初始化  */  public function init($dateTime=null){    if(!empty($dateTime)){ //当月      $this->curYear=date('Y',strtotime($dateTime));      $this->curMonth=date('n',strtotime($dateTime));      $this->curDay=date('j',strtotime($dateTime));    }    $this->curWek=date('w',strtotime($this->curYear.'-'.$this->curMonth.'-1'));    //上一个月    $this->lastMonth=$this->curMonth-1; //上一个月    $this->lastYear=$this->curYear; //上一个月属于哪一年    if($this->lastMonth<0){      $this->lastMonth=12;      $this->lastYear-=1;    }    //下一个月    $this->nextMonth=$this->curMonth+1;//下一个月    $this->nextYear=$this->curYear; //下一个月属于哪一年    if($this->nextMonth > 12){      $this->nextMonth=1;      $this->nextYear+=1;    }  }  public function getCalendar(){    return $this->calendar;  }  /**  *创建日历从周日 周一 周二 周三 周四 周五 周六,7*5方格,前面补上月后几天,后面补下月开始几天  **/  public function createCalendar(){    //判断当月共计多少天    $nextStr=$this->nextYear.'-'.$this->nextMonth.'-1 -1 days';    $curDaySum=date('j',strtotime($nextStr));    //判断上一个月最后一天是多少号    $lastStr=$this->curYear.'-'.$this->curMonth.'-1 -1 days';    $lastDaySum=date('j',strtotime($lastStr));    $prefixLId=$this->lastYear.'-'.$this->lastMonth;    $prefixCId=$this->curYear.'-'.$this->curMonth;    $prefixNId=$this->nextYear.'-'.$this->nextMonth;    if($this->curWek == 0){      $lastMonthSum=7; //需要添加上个月的$lastMonthSum天    }else{      $lastMonthSum=$this->curWek;    }    $lastMonthStart=$lastDaySum - $lastMonthSum+1;    for($i=0,$j=1,$k=1;$i<42;$i++){      $dateInfo=array();      if($i<$lastMonthSum){ //上一个月        $dateInfo['day']=$lastMonthStart + $i;        $dateInfo['type']=1;        $id=$prefixLId.'-'.$dateInfo['day'];        $this->calendar[]=array('id'=>$id,                    'info'=>$dateInfo);      }else if($j > $curDaySum){//下一个月        $id=$prefixNId.'-'.$k;        $dateInfo['day']=$k;        $dateInfo['type']=3;        $this->calendar[]=array('id'=>$id,                    'info'=>$dateInfo);        $k++;      }else{//本月        $dateInfo['day']=$j;        $dateInfo['type']=2;        $this->calendar[]=array('id'=>($prefixCId.'-'.$j),                    'info'=>$dateInfo);        $j++;        $this->curDaySum+=1;      }    }  }  public function getDayTime(){    return $this->curYear.'-'.$this->curMonth.'-'.$this->curDay;  }  /**  *获取当前月属于哪个月  **/  public function getCurMonth(){    return $this->curYear.'-'.$this->curMonth;  }  /**  *获取上一个月属于哪个月  **/  public function getLastMonth(){    return $this->lastYear.'-'.$this->lastMonth;  }  /**  *获取下一个月属于哪个月  **/  public function getNextMonth(){    return $this->nextYear.'-'.$this->nextMonth;  }  /**  *判断当前月有多少天  **/  public function getCurDaySum(){    return $this->curDaySum;  }}

WorkLog.class.php文件,主要负责将用户工作信息与日历信息结合起来。

uid=$uid;    $this->calendar=new \Util\CalenDar($daytime);    $this->lastMonth=$this->calendar->getLastMonth();    $this->curMonth=$this->calendar->getCurMonth();    $this->nextMonth=$this->calendar->getNextMonth();    $this->init();  }  public function init(){    $info=$this->calendar->getCalendar();    $userLog=array();    if($this->uid !=0){      $lastMonth=explode('-', $this->lastMonth);      $curMonth=explode('-', $this->curMonth);      $nextMonth=explode('-', $this->nextMonth);      //获取上一个月,当前月,下一个月的工作日志,后期使用缓存      $where='uid='.$this->uid.' AND ((`year`='.$lastMonth[0].' AND `month`='.$lastMonth[1].' ) or (`year`='.$curMonth[0].' AND `month`='.$curMonth[1].' ) or (`year`='.$nextMonth[0].' AND `month`='.$nextMonth[1].' ))';      $rs=M('work_log')->field('year,month,day,status')->where($where)->select();      foreach ($rs as $value) {        $userLog[$value['year'].'-'.$value['month'].'-'.$value['day']]=$value['status'];      }    }    $flag=1;    foreach ($info as $key=>$value) {      if($flag % 7 ==1 || $flag % 7 ==0){        $cellbgtype=3;//没有工作      }else{        $cellbgtype=1;//没有工作      }      if(isset($userLog[$value['id']]) && $userLog[$value['id']] ==1){        //判断是不是当前月份        $str=date('Y-n',strtotime($value['id']));        if($this->curMonth == $str){          $this->monthWorkedDays+=1;        }        $cellbgtype+=1;      }      $cellbgtype='daytype'.$cellbgtype.'_'.$value['info']['type'];      $info[$key]['info']['class']=$cellbgtype;      $flag++;    }    $this->workLog=$info;  }  public function getLastMonth(){    return $this->lastMonth;  }  public function getCurMonth(){    return $this->curMonth;  }  public function getNextMonth(){    return $this->nextMonth;  }  /**  *当月已经工作的天数  */  public function monthWorkedDays(){    return $this->monthWorkedDays;  }  /**  *当月的天数  **/  public function monthDays(){    return $this->calendar->getCurDaySum();  }  /**  *获取累计工作的天数  **/  public function workedDays(){  }  /**  *当前工作日历的月份  **/  public function logMonth(){  }  /**  *当月截止到目前可得薪水  **/  public function hadSalary(){  }  /**  *预计可得最高薪水  **/  public function maxSalary(){  }  /**  *当前的工作日历  **/  public function workInfo(){    return $this->workLog;  }}

调用文件

IndexController.class.php

getCurMonth();   $lastMDay=strtotime($curDay.' -1 month');   $nextMDay=strtotime($curDay.' +1 month');   $nowTime=date('当前时间:Y年m月d号 H:i:s');   $curDayStr=date('日历时间:Y年m月d号',strtotime($curDay));   $info=$WorkLog->workInfo();   $curDaySum=$WorkLog->monthDays();   $workDays=$WorkLog->monthWorkedDays();   $this->assign('lastMDay',$lastMDay);   $this->assign('nextMDay',$nextMDay);   $this->assign('nowTime',$nowTime);   $this->assign('curDay',$curDayStr);   $this->assign('info',$info);   $this->assign('curDaySum',$curDaySum);   $this->assign('workDays',$workDays);   $this->display();  }}

显示文件

index.html

日历  
  • {$nowTime}
    {$curDay}
周日 周一 周二 周三 周四 周五 周六
{$dayinfo['info']['day']}
  • 工作
  • 休息
  • 上一月下一月
  • 本月共计:{$curDaySum} 天
  • 本月已工作:{$workDays} 天
  • 本月剩余工作日:** 天
  • 预计工资:**天
  • 设置

思路分析

1.在CalenDar.class.php中,封装每个月的日期信息。如果读者需要做日历,只需要将该文件作为一个类调用即可。
如下图

$calendar=new \Util\CalenDar();$info=$calendar->getCalendar();echo json_encode($info);//输出结果如下[{"id":"2016-5-29","info":{"day":29,"type":1}},{"id":"2016-5-30","info":{"day":30,"type":1}},{"id":"2016-5-31","info":{"day":31,"type":1}},{"id":"2016-6-1","info":{"day":1,"type":2}},{"id":"2016-6-2","info":{"day":2,"type":2}},{"id":"2016-6-3","info":{"day":3,"type":2}},{"id":"2016-6-4","info":{"day":4,"type":2}},{"id":"2016-6-5","info":{"day":5,"type":2}},{"id":"2016-6-6","info":{"day":6,"type":2}},{"id":"2016-6-7","info":{"day":7,"type":2}},{"id":"2016-6-8","info":{"day":8,"type":2}},{"id":"2016-6-9","info":{"day":9,"type":2}},{"id":"2016-6-10","info":{"day":10,"type":2}},{"id":"2016-6-11","info":{"day":11,"type":2}},{"id":"2016-6-12","info":{"day":12,"type":2}},{"id":"2016-6-13","info":{"day":13,"type":2}},{"id":"2016-6-14","info":{"day":14,"type":2}},{"id":"2016-6-15","info":{"day":15,"type":2}},{"id":"2016-6-16","info":{"day":16,"type":2}},{"id":"2016-6-17","info":{"day":17,"type":2}},{"id":"2016-6-18","info":{"day":18,"type":2}},{"id":"2016-6-19","info":{"day":19,"type":2}},{"id":"2016-6-20","info":{"day":20,"type":2}},{"id":"2016-6-21","info":{"day":21,"type":2}},{"id":"2016-6-22","info":{"day":22,"type":2}},{"id":"2016-6-23","info":{"day":23,"type":2}},{"id":"2016-6-24","info":{"day":24,"type":2}},{"id":"2016-6-25","info":{"day":25,"type":2}},{"id":"2016-6-26","info":{"day":26,"type":2}},{"id":"2016-6-27","info":{"day":27,"type":2}},{"id":"2016-6-28","info":{"day":28,"type":2}},{"id":"2016-6-29","info":{"day":29,"type":2}},{"id":"2016-6-30","info":{"day":30,"type":2}},{"id":"2016-7-1","info":{"day":1,"type":3}},{"id":"2016-7-2","info":{"day":2,"type":3}},{"id":"2016-7-3","info":{"day":3,"type":3}},{"id":"2016-7-4","info":{"day":4,"type":3}},{"id":"2016-7-5","info":{"day":5,"type":3}},{"id":"2016-7-6","info":{"day":6,"type":3}},{"id":"2016-7-7","info":{"day":7,"type":3}},{"id":"2016-7-8","info":{"day":8,"type":3}},{"id":"2016-7-9","info":{"day":9,"type":3}}]

2.在WorkLog.class.php中,获取该用户上一个月、当前月、下一个月的工作信息,之所以使用一次性获取三个月的工作信息,因为如果每天的去读取,这样数据查询的次数过大,当然最好的还是做一下缓存比较好。读取到工作信息后,然后结合日历,判断每天是否工作,以及是否是周末,来决定日历中每个方格的背景样式。工作信息数据库如下图:

PS:这里再为大家分享几款本站的在线日期工具供大家参考:

在线万年历日历:
http://tools..net.cn/bianmin/wannianli

网页万年历日历:
http://tools..net.cn/bianmin/webwannianli

在线万年历黄历flash版:
http://tools..net.cn/bianmin/flashwnl

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》、《smarty模板入门基础教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

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


  • 上一条:
    Thinkphp5.0自动生成模块及目录的方法详解
    下一条:
    ThinkPHP中调用PHPExcel的实现代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

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

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

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

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

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

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

    侯体宗的博客