ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DateValue.php
Go to the documentation of this file.
1<?php
2
4
5use DateTimeImmutable;
7use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
8
10{
36 public static function fromString($dateValue)
37 {
38 $dti = new DateTimeImmutable();
39 $baseYear = SharedDateHelper::getExcelCalendar();
40 $dateValue = trim(Functions::flattenSingleValue($dateValue), '"');
41 // Strip any ordinals because they're allowed in Excel (English only)
42 $dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue) ?? '';
43 // Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany)
44 $dateValue = str_replace(['/', '.', '-', ' '], ' ', $dateValue);
45
46 $yearFound = false;
47 $t1 = explode(' ', $dateValue);
48 $t = '';
49 foreach ($t1 as &$t) {
50 if ((is_numeric($t)) && ($t > 31)) {
51 if ($yearFound) {
52 return Functions::VALUE();
53 }
54 if ($t < 100) {
55 $t += 1900;
56 }
57 $yearFound = true;
58 }
59 }
60 if (count($t1) === 1) {
61 // We've been fed a time value without any date
62 return ((strpos((string) $t, ':') === false)) ? Functions::Value() : 0.0;
63 }
64 unset($t);
65
66 $dateValue = self::t1ToString($t1, $dti, $yearFound);
67
68 $PHPDateArray = self::setUpArray($dateValue, $dti);
69
70 return self::finalResults($PHPDateArray, $dti, $baseYear);
71 }
72
73 private static function t1ToString(array $t1, DateTimeImmutable $dti, bool $yearFound): string
74 {
75 if (count($t1) == 2) {
76 // We only have two parts of the date: either day/month or month/year
77 if ($yearFound) {
78 array_unshift($t1, 1);
79 } else {
80 if (is_numeric($t1[1]) && $t1[1] > 29) {
81 $t1[1] += 1900;
82 array_unshift($t1, 1);
83 } else {
84 $t1[] = $dti->format('Y');
85 }
86 }
87 }
88 $dateValue = implode(' ', $t1);
89
90 return $dateValue;
91 }
92
98 private static function setUpArray(string $dateValue, DateTimeImmutable $dti)
99 {
100 $PHPDateArray = date_parse($dateValue);
101 if (($PHPDateArray === false) || ($PHPDateArray['error_count'] > 0)) {
102 // If original count was 1, we've already returned.
103 // If it was 2, we added another.
104 // Therefore, neither of the first 2 stroks below can fail.
105 $testVal1 = strtok($dateValue, '- ');
106 $testVal2 = strtok('- ');
107 $testVal3 = strtok('- ') ?: $dti->format('Y');
108 Helpers::adjustYear((string) $testVal1, (string) $testVal2, $testVal3);
109 $PHPDateArray = date_parse($testVal1 . '-' . $testVal2 . '-' . $testVal3);
110 if (($PHPDateArray === false) || ($PHPDateArray['error_count'] > 0)) {
111 $PHPDateArray = date_parse($testVal2 . '-' . $testVal1 . '-' . $testVal3);
112 }
113 }
114
115 return $PHPDateArray;
116 }
117
126 private static function finalResults($PHPDateArray, DateTimeImmutable $dti, int $baseYear)
127 {
128 $retValue = Functions::Value();
129 if (is_array($PHPDateArray) && $PHPDateArray['error_count'] == 0) {
130 // Execute function
131 Helpers::replaceIfEmpty($PHPDateArray['year'], $dti->format('Y'));
132 if ($PHPDateArray['year'] < $baseYear) {
133 return Functions::VALUE();
134 }
135 Helpers::replaceIfEmpty($PHPDateArray['month'], $dti->format('m'));
136 Helpers::replaceIfEmpty($PHPDateArray['day'], $dti->format('d'));
137 $PHPDateArray['hour'] = 0;
138 $PHPDateArray['minute'] = 0;
139 $PHPDateArray['second'] = 0;
140 $month = (int) $PHPDateArray['month'];
141 $day = (int) $PHPDateArray['day'];
142 $year = (int) $PHPDateArray['year'];
143 if (!checkdate($month, $day, $year)) {
144 return ($year === 1900 && $month === 2 && $day === 29) ? Helpers::returnIn3FormatsFloat(60.0) : Functions::VALUE();
145 }
146 $retValue = Helpers::returnIn3FormatsArray($PHPDateArray, true);
147 }
148
149 return $retValue;
150 }
151}
An exception for terminatinating execution or to throw for unit testing.
static t1ToString(array $t1, DateTimeImmutable $dti, bool $yearFound)
Definition: DateValue.php:73
static finalResults($PHPDateArray, DateTimeImmutable $dti, int $baseYear)
Final results.
Definition: DateValue.php:126
static setUpArray(string $dateValue, DateTimeImmutable $dti)
Parse date.
Definition: DateValue.php:98
static returnIn3FormatsArray(array $dateArray, bool $noFrac=false)
Return result in one of three formats.
Definition: Helpers.php:138
static replaceIfEmpty(&$value, $altValue)
Help reduce perceived complexity of some tests.
Definition: Helpers.php:114
static returnIn3FormatsFloat(float $excelDateValue)
Return result in one of three formats.
Definition: Helpers.php:173
static adjustYear(string $testVal1, string $testVal2, string &$testVal3)
Adjust year in ambiguous situations.
Definition: Helpers.php:122
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649