Home  >  Article  >  Backend Development  >  Calculate the interval from Monday to Friday within a year

Calculate the interval from Monday to Friday within a year

WBOY
WBOYOriginal
2016-07-25 09:08:311295browse
  1. //year
  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 means Monday, $nIndex==7 means Sunday
  13. $nIndex = date( "N",$time);
  14. //$mulNum=1 means Monday is one day later,
  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. //Week 1
  27. $mulNum = 7;
  28. }
  29. if($mulNum>2){
  30. $start = $time;
  31. / /Friday time
  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'])."week".date("N",$week['start']).">>>>>>".date("Y-m-d",$week[' end'])."week".date("N",$week['end']).'
    ';
  50. }
  51. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn