ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NetworkDays.php
Go to the documentation of this file.
1<?php
2
4
7
9{
29 public static function count($startDate, $endDate, ...$dateArgs)
30 {
31 try {
32 // Retrieve the mandatory start and end date that are referenced in the function definition
33 $sDate = Helpers::getDateValue($startDate);
34 $eDate = Helpers::getDateValue($endDate);
35 $startDate = min($sDate, $eDate);
36 $endDate = max($sDate, $eDate);
37 // Get the optional days
38 $dateArgs = Functions::flattenArray($dateArgs);
39 // Test any extra holiday parameters
40 $holidayArray = [];
41 foreach ($dateArgs as $holidayDate) {
42 $holidayArray[] = Helpers::getDateValue($holidayDate);
43 }
44 } catch (Exception $e) {
45 return $e->getMessage();
46 }
47
48 // Execute function
49 $startDow = self::calcStartDow($startDate);
50 $endDow = self::calcEndDow($endDate);
51 $wholeWeekDays = (int) floor(($endDate - $startDate) / 7) * 5;
52 $partWeekDays = self::calcPartWeekDays($startDow, $endDow);
53
54 // Test any extra holiday parameters
55 $holidayCountedArray = [];
56 foreach ($holidayArray as $holidayDate) {
57 if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
58 if ((Week::day($holidayDate, 2) < 6) && (!in_array($holidayDate, $holidayCountedArray))) {
59 --$partWeekDays;
60 $holidayCountedArray[] = $holidayDate;
61 }
62 }
63 }
64
65 return self::applySign($wholeWeekDays + $partWeekDays, $sDate, $eDate);
66 }
67
68 private static function calcStartDow(float $startDate): int
69 {
70 $startDow = 6 - (int) Week::day($startDate, 2);
71 if ($startDow < 0) {
72 $startDow = 5;
73 }
74
75 return $startDow;
76 }
77
78 private static function calcEndDow(float $endDate): int
79 {
80 $endDow = (int) Week::day($endDate, 2);
81 if ($endDow >= 6) {
82 $endDow = 0;
83 }
84
85 return $endDow;
86 }
87
88 private static function calcPartWeekDays(int $startDow, int $endDow): int
89 {
90 $partWeekDays = $endDow + $startDow;
91 if ($partWeekDays > 5) {
92 $partWeekDays -= 5;
93 }
94
95 return $partWeekDays;
96 }
97
98 private static function applySign(int $result, float $sDate, float $eDate): int
99 {
100 return ($sDate > $eDate) ? -$result : $result;
101 }
102}
$result
An exception for terminatinating execution or to throw for unit testing.
static getDateValue($dateValue, bool $allowBool=true)
getDateValue.
Definition: Helpers.php:31
static applySign(int $result, float $sDate, float $eDate)
Definition: NetworkDays.php:98
static count($startDate, $endDate,... $dateArgs)
NETWORKDAYS.
Definition: NetworkDays.php:29
static day($dateValue, $style=1)
WEEKDAY.
Definition: Week.php:131
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583