PHP code to calculate the number of weekend days in a period of time (Saturday Sunday sum)

  • 2020-03-31 16:43:21
  • OfStack

 
 
function get_weekend_days($start_date,$end_date){ 

if (strtotime($start_date) > strtotime($end_date)) list($start_date, $end_date) = array($end_date, $start_date); 

$start_reduce = $end_add = 0; 

$start_N = date('N',strtotime($start_date)); 
$start_reduce = ($start_N == 7) ? 1 : 0; 

$end_N = date('N',strtotime($end_date)); 
in_array($end_N,array(6,7)) && $end_add = ($end_N == 7) ? 2 : 1; 

$days = abs(strtotime($end_date) - strtotime($start_date))/86400 + 1; 

return floor(($days + $start_N - 1 - $end_N) / 7) * 2 - $start_reduce + $end_add; 
} 

Remark:

Recently wrote to the company with the attendance system, one of the functions of automation, is the working day of each month (attendance days) to automatically write, then write the above function, used to calculate the total number of Saturday and Sunday in the two dates, explain it a little, this feature is of course is the easiest to implement in circulation, from the start for to the end of the day, that day in the middle as long as it is Saturday or Sunday, + +, and finally calculate the total easily, but still that sentence, the cycle efficiency is really bad, especially when the time span is too long, gloomy.

My basic idea of this function is four words: before complement after chop. You don't understand? I'm a little confused too... Is the start date on the number of weeks, if less than a week, then fill the corresponding number of days, such as the start date is week 3, then the total number of days in 2 days (week 1 and week 2), if the start date is week 6, then make up five days, that is 6-1, is in the function $start_N - 1, if the start date is on Sunday, so make up for all the 6 days at the same time, the final result need to minus one day (Saturday), which is in the function $start_reduce, ok, now "before" explanation is done. Let's talk about "backslash", as the name implies, it means to cut off the number of days less than one week, for example, the end date is 3 days of the week, so subtract 3 days from the total number of days, if the end date is 6 days or Sunday, then subtract 6 or 7, and at the same time add 1 or 2 at the end.

There's nothing hard about the algorithm, but the idea is to take this time period and make it an integer of 7, multiply it by 2, subtract or add more and less Saturday or Sunday, and you get the sum of Saturday and Sunday. Finally, it is not recommended to use date(z) to calculate the number of days in a certain period of time, because the generality will be bad, involving the problem of the New Year, if over many years, but also to consider the leap year problem, it is more straightforward to calculate so.

Improve the record , add the $is_workday parameter, you can choose whether to return to work days, the default is to return to rest days
 
function get_weekend_days($start_date,$end_date,$is_workday = false){ 

if (strtotime($start_date) > strtotime($end_date)) list($start_date, $end_date) = array($end_date, $start_date); 
$start_reduce = $end_add = 0; 
$start_N = date('N',strtotime($start_date)); 
$start_reduce = ($start_N == 7) ? 1 : 0; 
$end_N = date('N',strtotime($end_date)); 
in_array($end_N,array(6,7)) && $end_add = ($end_N == 7) ? 2 : 1; 
$alldays = abs(strtotime($end_date) - strtotime($start_date))/86400 + 1; 
$weekend_days = floor(($alldays + $start_N - 1 - $end_N) / 7) * 2 - $start_reduce + $end_add; 
if ($is_workday){ 
$workday_days = $alldays - $weekend_days; 
return $workday_days; 
} 
return $weekend_days; 
} 

Related articles: