ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Week.php
Go to the documentation of this file.
1<?php
2
4
5use DateTime;
8use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
9
10class Week
11{
41 public static function number($dateValue, $method = Constants::STARTWEEK_SUNDAY)
42 {
43 $origDateValueNull = empty($dateValue);
44
45 try {
46 $method = self::validateMethod($method);
47 if ($dateValue === null) { // boolean not allowed
48 $dateValue = (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_MAC_1904 || $method === Constants::DOW_SUNDAY) ? 0 : 1;
49 }
50 $dateValue = self::validateDateValue($dateValue);
51 if (!$dateValue && self::buggyWeekNum1900($method)) {
52 // This seems to be an additional Excel bug.
53 return 0;
54 }
55 } catch (Exception $e) {
56 return $e->getMessage();
57 }
58
59 // Execute function
60 $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
61 if ($method == Constants::STARTWEEK_MONDAY_ISO) {
62 Helpers::silly1900($PHPDateObject);
63
64 return (int) $PHPDateObject->format('W');
65 }
66 if (self::buggyWeekNum1904($method, $origDateValueNull, $PHPDateObject)) {
67 return 0;
68 }
69 Helpers::silly1900($PHPDateObject, '+ 5 years'); // 1905 calendar matches
70 $dayOfYear = (int) $PHPDateObject->format('z');
71 $PHPDateObject->modify('-' . $dayOfYear . ' days');
72 $firstDayOfFirstWeek = (int) $PHPDateObject->format('w');
73 $daysInFirstWeek = (6 - $firstDayOfFirstWeek + $method) % 7;
74 $daysInFirstWeek += 7 * !$daysInFirstWeek;
75 $endFirstWeek = $daysInFirstWeek - 1;
76 $weekOfYear = floor(($dayOfYear - $endFirstWeek + 13) / 7);
77
78 return (int) $weekOfYear;
79 }
80
94 public static function isoWeekNumber($dateValue)
95 {
96 if (self::apparentBug($dateValue)) {
97 return 52;
98 }
99
100 try {
101 $dateValue = Helpers::getDateValue($dateValue);
102 } catch (Exception $e) {
103 return $e->getMessage();
104 }
105
106 // Execute function
107 $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
108 Helpers::silly1900($PHPDateObject);
109
110 return (int) $PHPDateObject->format('W');
111 }
112
131 public static function day($dateValue, $style = 1)
132 {
133 try {
134 $dateValue = Helpers::getDateValue($dateValue);
136 } catch (Exception $e) {
137 return $e->getMessage();
138 }
139
140 // Execute function
141 $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
142 Helpers::silly1900($PHPDateObject);
143 $DoW = (int) $PHPDateObject->format('w');
144
145 switch ($style) {
146 case 1:
147 ++$DoW;
148
149 break;
150 case 2:
151 $DoW = self::dow0Becomes7($DoW);
152
153 break;
154 case 3:
155 $DoW = self::dow0Becomes7($DoW) - 1;
156
157 break;
158 }
159
160 return $DoW;
161 }
162
166 private static function validateStyle($style): int
167 {
169
170 if (!is_numeric($style)) {
171 throw new Exception(Functions::VALUE());
172 }
173 $style = (int) $style;
174 if (($style < 1) || ($style > 3)) {
175 throw new Exception(Functions::NAN());
176 }
177
178 return $style;
179 }
180
181 private static function dow0Becomes7(int $DoW): int
182 {
183 return ($DoW === 0) ? 7 : $DoW;
184 }
185
190 private static function apparentBug($dateValue): bool
191 {
192 if (SharedDateHelper::getExcelCalendar() !== SharedDateHelper::CALENDAR_MAC_1904) {
193 if (is_bool($dateValue)) {
194 return true;
195 }
196 if (is_numeric($dateValue) && !((int) $dateValue)) {
197 return true;
198 }
199 }
200
201 return false;
202 }
203
209 private static function validateDateValue($dateValue): float
210 {
211 if (is_bool($dateValue)) {
212 throw new Exception(Functions::VALUE());
213 }
214
215 return Helpers::getDateValue($dateValue);
216 }
217
223 private static function validateMethod($method): int
224 {
225 if ($method === null) {
227 }
228 $method = Functions::flattenSingleValue($method);
229 if (!is_numeric($method)) {
230 throw new Exception(Functions::VALUE());
231 }
232
233 $method = (int) $method;
234 if (!array_key_exists($method, Constants::METHODARR)) {
235 throw new Exception(Functions::NAN());
236 }
237 $method = Constants::METHODARR[$method];
238
239 return $method;
240 }
241
242 private static function buggyWeekNum1900(int $method): bool
243 {
244 return $method === Constants::DOW_SUNDAY && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900;
245 }
246
247 private static function buggyWeekNum1904(int $method, bool $origNull, DateTime $dateObject): bool
248 {
249 // This appears to be another Excel bug.
250
251 return $method === Constants::DOW_SUNDAY && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_MAC_1904 &&
252 !$origNull && $dateObject->format('Y-m-d') === '1904-01-01';
253 }
254}
An exception for terminatinating execution or to throw for unit testing.
static getDateValue($dateValue, bool $allowBool=true)
getDateValue.
Definition: Helpers.php:31
static silly1900(DateTime $PHPDateObject, string $mod='-1 day')
Definition: Helpers.php:278
static number($dateValue, $method=Constants::STARTWEEK_SUNDAY)
WEEKNUM.
Definition: Week.php:41
static buggyWeekNum1904(int $method, bool $origNull, DateTime $dateObject)
Definition: Week.php:247
static validateDateValue($dateValue)
Validate dateValue parameter.
Definition: Week.php:209
static isoWeekNumber($dateValue)
ISOWEEKNUM.
Definition: Week.php:94
static validateMethod($method)
Validate method parameter.
Definition: Week.php:223
static day($dateValue, $style=1)
WEEKDAY.
Definition: Week.php:131
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
$style
Definition: example_012.php:70