首頁 >後端開發 >php教程 >計算一年內週一到週五的間隔區間

計算一年內週一到週五的間隔區間

WBOY
WBOY原創
2016-07-25 09:08:311355瀏覽
  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<=$endTime;){
  9. $forTime++;
  10. $start = $end = 0;
  11. if($isBegin){
  12. //$nIndex==1表示星期一,$nIndex==7表示星期日
  13. $nIndex = date("N",$time);
  14. //$mulNum=1表示过1天就是星期一,
  15. $mulNum = 0;
  16. switch ($nIndex){
  17. case 1: $mulNum =7; break;
  18. case 2: $mulNum =6; break;
  19. case 3: $mulNum =5; break;
  20. case 4: $mulNum =4; break;
  21. case 5: $mulNum =3; break;
  22. case 6: $mulNum =2; break;
  23. case 7: $mulNum =1; break;
  24. }
  25. }else{
  26. //周1
  27. $mulNum = 7;
  28. }
  29. if($mulNum>2){
  30. $start = $time;
  31. //周五时间
  32. $end = $time+($mulNum-2-1)*24*60*60;
  33. if($end>$endTime){
  34. $end = $endTime;
  35. }
  36. $weekArray[] = array(
  37. 'start'=>$start,
  38. 'end'=> $end,
  39. );
  40. if($end>=$endTime){
  41. break;
  42. }
  43. }
  44. $time += $mulNum*24*60*60;
  45. $isBegin = false;
  46. }
  47. echo $forTime.'
    ';
  48. foreach ($weekArray as $week){
  49. echo date("Y-m-d",$week['start'])."星期".date("N",$week['start']).">>>>>>".date("Y-m-d",$week['end'])."星期".date("N",$week['end']).'
    ';
  50. }
  51. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn