Guest User

Untitled

a guest
Dec 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Return days between two dates
  5. *
  6. * @param string $startDate
  7. * @param string $endDate
  8. * @param array $holidays
  9. * @return integer
  10. */
  11. function getWorkingDays(string $startDate, string $endDate, array $holidays = []): int
  12. {
  13. $begin = strtotime($startDate);
  14. $end = strtotime($endDate);
  15.  
  16. if ($begin > $end) {
  17. return 0;
  18. } else {
  19. $weekends = 0;
  20. $noDays = 0;
  21. $holidayCount = 0;
  22.  
  23. while ($begin <= $end) {
  24. if (in_array(date("d/m", $begin), $holidays)) {
  25. $holidayCount++;
  26. }
  27.  
  28. /* 6 and 7 are weekend days */
  29. if (date("N", $begin) > 5) {
  30. $weekends++;
  31. }
  32.  
  33. /* +1 day */
  34. $begin += 86400;
  35. /*no of days in the given interval*/
  36. $noDays++;
  37. }
  38.  
  39. return $noDays - $weekends - $holidayCount;
  40. }
  41. }
Add Comment
Please, Sign In to add comment