首页 >后端开发 >php教程 >计算一年内周一到周五的间隔区间

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

WBOY
WBOY原创
2016-07-25 09:08:311353浏览
  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. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn