BUSINESS DAYS OF A MONTH AND YEAR IN PHP

Today my blog is about the business working days in a week,  so the normal business days are from Monday to Friday. So the output of this blog will give you as well as it will give you the week number of each month. For, getting those data we need tow parameters ie..,  month as the first parameter and year as the second parameter. This will give you the output as an array only in the format of Year-Month-Date.  This will avoid every Saturday and Sunday of every month

public static function getWeeks($month,$year)
 {
  $date = new \DateTime("now");
  $date->setDate($year, $month, 1);
  $date->setTime(0, 0, 0);
  $maxDay = intval($date->format("t"));
  $dayOfTheWeek = intval($date->format("N"));
  if($dayOfTheWeek != 1) {
   $diff = 8 - $dayOfTheWeek;
   if($dayOfTheWeek <= 5) {
    $from = $date->format("Y-m-d");
    $diff2 = 5 - $dayOfTheWeek;
    $date->modify(sprintf("+%d days", $diff2));
    $to = $date->format("Y-m-d");
    $diff -= $diff2;
   }
   $date->modify(sprintf("+%d days", $diff));
  }
  $week_range = [];
  while(intval($date->format("n")) == $month) {
   $i = 0;
   $from = $date->format("Y-m-d");
      $date->modify("+4 days");
      if(intval($date->format("n")) > $month) {
          $date->setDate($year, $month, $maxDay);
      }
   $to = $date->format("Y-m-d");
   $date->modify("+3 days");
   $ar = array("start_date"=>$from,"end_date"=>$to);
   array_push($week_range, $ar);
  }
  return $week_range;
 }

Comments