Code get number day in month

Code get number day in month

Share for everyone code get number day of a month.
Below, I code with PHP.

Code get number day in month

Format number of the day: start from 0 -> Sunday, end at 6 -> Satuday

0 – Sunday
1 – Monday
2 – Tuesday
3 – Wednesday
4 – Thursday
5 – Friday
6 – Satuday

/*
*$start format: yyyy-mm-dd
*$end format: yyyy-mm-dd
*/
public function numberDayInMonth($start, $end) {
	$dayInWeek           = array(1, 2, 3, 4, 5, 6, 0);
	foreach($dayInWeek as $indexDay){
		$result[$indexDay] = 0;
	}
	$start = strtotime($start);
	$end = strtotime($end);
	//get interval day from param $start and $end
	$intervalDay = (int)($end - $start)/86400;
	//for each day
	for($i = 0; $i <= $intervalDay; $i++) {
		$w = date("w", $start); //$w: format number of day
		//set total number of day
		if(isset($result[$w])){
			$result[$w] +=1;
		}else{
			$result[$w] =1;
		}
		//increase start day
		$start = strtotime("+1 day", $start);
	}
	return $result;
}

 

 

//show result

Array
(
    [1] => 5
    [2] => 4
    [3] => 4
    [4] => 4
    [5] => 4
    [6] => 4
    [0] => 4
)

 

Thanks for watching!