ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DateParts.php
Go to the documentation of this file.
1<?php
2
4
7use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
8
10{
25 public static function day($dateValue)
26 {
27 $weirdResult = self::weirdCondition($dateValue);
28 if ($weirdResult >= 0) {
29 return $weirdResult;
30 }
31
32 try {
33 $dateValue = Helpers::getDateValue($dateValue);
34 } catch (Exception $e) {
35 return $e->getMessage();
36 }
37
38 // Execute function
39 $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
40
41 return (int) $PHPDateObject->format('j');
42 }
43
58 public static function month($dateValue)
59 {
60 try {
61 $dateValue = Helpers::getDateValue($dateValue);
62 } catch (Exception $e) {
63 return $e->getMessage();
64 }
65 if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
66 return 1;
67 }
68
69 // Execute function
70 $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
71
72 return (int) $PHPDateObject->format('n');
73 }
74
89 public static function year($dateValue)
90 {
91 try {
92 $dateValue = Helpers::getDateValue($dateValue);
93 } catch (Exception $e) {
94 return $e->getMessage();
95 }
96
97 if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
98 return 1900;
99 }
100 // Execute function
101 $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
102
103 return (int) $PHPDateObject->format('Y');
104 }
105
110 private static function weirdCondition($dateValue): int
111 {
112 // Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR)
113 if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
114 if (is_bool($dateValue)) {
115 return (int) $dateValue;
116 }
117 if ($dateValue === null) {
118 return 0;
119 }
120 if (is_numeric($dateValue) && $dateValue < 1 && $dateValue >= 0) {
121 return 0;
122 }
123 }
124
125 return -1;
126 }
127}
An exception for terminatinating execution or to throw for unit testing.
static getDateValue($dateValue, bool $allowBool=true)
getDateValue.
Definition: Helpers.php:31
static getCompatibilityMode()
Return the current Compatibility Mode.
Definition: Functions.php:93