欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

计算一年内周一到周五的间隔区间

程序员文章站 2022-04-11 13:00:35
...
  1. //年
  2. $year = date("Y");
  3. $beninTime = mktime(0,0,0,1,1,$year);
  4. $endTime = mktime(0,0,0,1,1,$year+1)-1;
  5. $weekArray = array();
  6. $isBegin = true;
  7. $forTime = 0;
  8. for ($time=$beninTime;$time $forTime++;
  9. $start = $end = 0;
  10. if($isBegin){
  11. //$nIndex==1表示星期一,$nIndex==7表示星期日
  12. $nIndex = date("N",$time);
  13. //$mulNum=1表示过1天就是星期一,
  14. $mulNum = 0;
  15. switch ($nIndex){
  16. case 1: $mulNum =7; break;
  17. case 2: $mulNum =6; break;
  18. case 3: $mulNum =5; break;
  19. case 4: $mulNum =4; break;
  20. case 5: $mulNum =3; break;
  21. case 6: $mulNum =2; break;
  22. case 7: $mulNum =1; break;
  23. }
  24. }else{
  25. //周1
  26. $mulNum = 7;
  27. }
  28. if($mulNum>2){
  29. $start = $time;
  30. //周五时间
  31. $end = $time+($mulNum-2-1)*24*60*60;
  32. if($end>$endTime){
  33. $end = $endTime;
  34. }
  35. $weekArray[] = array(
  36. 'start'=>$start,
  37. 'end'=> $end,
  38. );
  39. if($end>=$endTime){
  40. break;
  41. }
  42. }
  43. $time += $mulNum*24*60*60;
  44. $isBegin = false;
  45. }
  46. echo $forTime.'
    ';
  47. foreach ($weekArray as $week){
  48. echo date("Y-m-d",$week['start'])."星期".date("N",$week['start']).">>>>>>".date("Y-m-d",$week['end'])."星期".date("N",$week['end']).'
    ';
  49. }
  50. ?>
复制代码