PHP日程计划管理功能之日期计算器(1)

最近为了写日期计算器,真真把自己绕的晕晕乎乎的。要计算隔一年,隔几个月后第几个星期,星期几等等。不是给定一个日期来获取这个日期是一年中的第几天或对应星期几。

PHP日程计划管理功能之日期计算器

因此为了方便后期开发日程管理类的功能,波波特意把本次项目中日程计划管理相关功能页面记录一个完整的笔记。本篇仅记录日程计划中最核心的日期计算器。

源码:

  1. /*
  2.  * Create time by Json
  3.  * @param $start int|string 开始时间
  4.  * @param $sched array 日期规则
  5.  * @return function(obj) 计算方法
  6.  */
  7. function create_time_from_json($start=null,$sched){
  8.     if(is_null($start)) {$start = time();}else{$start = strtotime($start) >time()?strtotime($start):time();}
  9.     switch ($sched['type']){
  10.         case 'year':
  11.             if($sched['cycle_type'] == 'week'){
  12.                 //{"type":"year","cycle_type":"week","gap":"1","month":"3","week":"3","weekday":"wednesday","begin_time":"10:00"}
  13.                 $format = function($sched,$start){
  14.                     $weeken = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  15.                     $year = date('Y',strtotime("+".$sched['gap']."year",strtotime($start)));
  16.                     $week = get_weekinfo($year.'-'.$sched['month']);
  17.                     $datetime = $week[$sched['week']-1][array_search($sched['weekday'],$weeken)]." ".$sched['begin_time'].":00";
  18.                     return $datetime;
  19.                 };
  20.             }
  21.             if($sched['cycle_type'] == 'month'){
  22.                 //{"type":"year","cycle_type":"month","gap":"1","month":"3","day":"2","begin_time":"10:00"}
  23.                 $format = function($sched,$start){
  24.                     $year = date('Y',strtotime("+".$sched['gap']."year",strtotime($start)));
  25.                     $sched['month'] = strlen($sched['month'])<2?"0".$sched['month']:$sched['month'];
  26.                     $sched['day'] = strlen($sched['day'])<2?"0".$sched['day']:$sched['day'];
  27.                     $datetime = implode("-",[$year,$sched['month'],$sched['day']])." ".$sched['begin_time'].":00";
  28.                     return $datetime;
  29.                 };
  30.             }
  31.             break;
  32.         case 'month':
  33.             if($sched['cycle_type'] == 'week'){
  34.                 //{"type":"month","cycle_type":"week","gap":"1","week":"1","weekday":"wednesday","begin_time":"10:00"}
  35.                 $format = function($sched,$start){
  36.                     $weeken = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  37.                     $ym = date('Y-m',strtotime("+".$sched['gap']."month",strtotime($start)));
  38.                     $week = get_weekinfo($ym);
  39.                     $datetime = $week[$sched['week']-1][array_search($sched['weekday'],$weeken)]." ".$sched['begin_time'].":00";
  40.                     return $datetime;
  41.                 };
  42.             }
  43.             if($sched['cycle_type'] == 'day'){
  44.                 //{"type":"month","cycle_type":"day","gap":"1","day":"4","begin_time":"10:00"}
  45.                 $format = function($sched,$start){
  46.                     $ym = date('Y-m',strtotime($start."+".$sched['gap']."month"));
  47.                     $sched['day'] = strlen($sched['day'])<2?"0".$sched['day']:$sched['day'];
  48.                     $datetime = $ym."-".$sched['day']." ".$sched['begin_time'].":00";
  49.                     return $datetime;
  50.                 };
  51.             }
  52.             break;
  53.         case 'week':
  54.             //{"type":"week","gap":"2","begin_time":"12:00","week":{"tuesday":1,"wednesday":1,"friday":1}}
  55.             $format = function ($sched,$start){
  56.                 $weeken = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
  57.                 $ymd = date('Y-m-d',strtotime("+".$sched['gap']."week",strtotime($start)));
  58.                 $week = get_weekinfo(mb_substr($ymd,0,7));
  59.                 foreach ($week as $key=>$value){
  60.                     if(in_array($ymd,$value)){
  61.                         foreach ($sched['week'] as $k=>$v){
  62.                             $datetime[] = $value[array_search($k,$weeken)]." ".$sched['begin_time'];
  63.                         }
  64.                     }
  65.                 }
  66.                 return $datetime;
  67.             };
  68.             break;
  69.         case 'hour':
  70.             //传入start参数须带时间!!!!
  71.             $format = function ($sched,$start){
  72.                 $datetime = date('Y-m-d H:i:s',strtotime("+".$sched['gap'].$sched['type'],strtotime($start)));
  73.                 return $datetime;
  74.             };
  75.             break;
  76.         case 'day':
  77.             $format = function ($sched,$start){
  78.                 $datetime = date('Y-m-d',strtotime("+".$sched['gap'].$sched['type'],strtotime($start)))." ".$sched['begin_time'].":00";
  79.                 return $datetime;
  80.             };
  81.             break;
  82.         default:
  83.             $format = function ($sched,$start){
  84.                 return "非法的字符串";
  85.             };
  86.     }
  87.     return $format;
  88. }
  89. /*
  90.  * 计算某一年某个月有几周
  91.  * @param $month string 年月
  92.  * @return array
  93.  * @notify 严禁传入日!!!
  94.  */
  95. function get_weekinfo($month){
  96.     $weekinfo = [];
  97.     $end_date = date('d',strtotime($month.' +1 month -1 day'));//计算当前月有多少天
  98.     for ($i=1; $i <$end_date ; $i=$i+7) {
  99.         $w = date('N',strtotime($month.'-'.$i));
  100.         $weekinfo[] = array(date('Y-m-d',strtotime($month.'-'.$i.' -'.($w-1).' days')),date('Y-m-d',strtotime($month.'-'.$i.' +'.(7-$w).' days')));
  101.     }
  102.     //填充每一周中间的日期
  103.     foreach($weekinfo as $key=>$value){
  104.         for($i=1;$i<7;$i++){
  105.             $weekinfo[$key][$i] = date('Y-m-d',strtotime($value[0].'+'.$i.' days'));
  106.         }
  107.     }
  108.     return $weekinfo;
  109. }

要对上述功能进行测试可以使用如下代码进行逐一测试。

  1. public function test(){
  2.         $json = '{"type":"year","cycle_type":"month","gap":"1","month":"3","day":"2","begin_time":"10:00"}';
  3.         $start = "2021-02-07 16:41:00";
  4.         $begin = "2021-02-01 00:00:00";
  5.         $end = "2021-02-28 00:00:00";
  6.         $data = array();
  7.         $schedule = json_decode($json,true);
  8.         $format = create_time_from_json($start,$schedule);//获取时间计算函数
  9.         while(strtotime($begin)<strtotime($start) && strtotime($start) < strtotime($end)){
  10.             $temp = $format($schedule,$start);
  11.             if(is_array($temp)){
  12.                 foreach ($temp as $key=>$vo){
  13.                     array_push($data,$vo);
  14.                 }
  15.             }else{
  16.                 array_push($data,$temp);
  17.             }
  18.             $start = end($data);
  19.         }
  20.         echo "<pre>";
  21.         print_r($data);
  22.     }

关于JSON字符串的解释:(关于解释可参考文章最上面的图)

①type:计划类型,

②cycle_tyle:循环类型,一部分日程计划下面有循环类型,一部分没有。主要看第一个type的单位大不大。比如按运行时长的循环单位已经是小时了,所以下面就没必要再划分间隔的分钟数等

③gap:间隔周期,字段一样,计划类型不同代表的意义不同。

④month/day:月份和日,这没什么好解释的

⑤week/weekday:第几周和周几。

特别提示:

1、在上述示例中返回的数据其实是一个计算日期的方法。根据存储的日程JSON数据,获得对应日程日期的计算方法,然后根据计划的开始时间和结束时间再获取计划内应该安排的日程日期。

2、上述方法获得的日期为字符串格式,不是时间戳。字符串与时间戳之间的转换相信难不倒任何一个PHPer

随后波波也会分享日程页面的HTML以及完整的存储过程,敬请期待。

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!