1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
8
|
|
|
|
9
|
|
|
class DateParts |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* DAYOFMONTH. |
13
|
|
|
* |
14
|
|
|
* Returns the day of the month, for a specified date. The day is given as an integer |
15
|
|
|
* ranging from 1 to 31. |
16
|
|
|
* |
17
|
|
|
* Excel Function: |
18
|
|
|
* DAY(dateValue) |
19
|
|
|
* |
20
|
|
|
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
21
|
|
|
* PHP DateTime object, or a standard date string |
22
|
|
|
* |
23
|
|
|
* @return int|string Day of the month |
24
|
|
|
*/ |
25
|
106 |
|
public static function day($dateValue) |
26
|
|
|
{ |
27
|
106 |
|
$weirdResult = self::weirdCondition($dateValue); |
28
|
106 |
|
if ($weirdResult >= 0) { |
29
|
5 |
|
return $weirdResult; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
try { |
33
|
101 |
|
$dateValue = Helpers::getDateValue($dateValue); |
34
|
3 |
|
} catch (Exception $e) { |
35
|
3 |
|
return $e->getMessage(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Execute function |
39
|
98 |
|
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue); |
40
|
|
|
|
41
|
98 |
|
return (int) $PHPDateObject->format('j'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* MONTHOFYEAR. |
46
|
|
|
* |
47
|
|
|
* Returns the month of a date represented by a serial number. |
48
|
|
|
* The month is given as an integer, ranging from 1 (January) to 12 (December). |
49
|
|
|
* |
50
|
|
|
* Excel Function: |
51
|
|
|
* MONTH(dateValue) |
52
|
|
|
* |
53
|
|
|
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
54
|
|
|
* PHP DateTime object, or a standard date string |
55
|
|
|
* |
56
|
|
|
* @return int|string Month of the year |
57
|
|
|
*/ |
58
|
114 |
|
public static function month($dateValue) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
114 |
|
$dateValue = Helpers::getDateValue($dateValue); |
62
|
2 |
|
} catch (Exception $e) { |
63
|
2 |
|
return $e->getMessage(); |
64
|
|
|
} |
65
|
112 |
|
if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) { |
66
|
1 |
|
return 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Execute function |
70
|
111 |
|
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue); |
71
|
|
|
|
72
|
111 |
|
return (int) $PHPDateObject->format('n'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* YEAR. |
77
|
|
|
* |
78
|
|
|
* Returns the year corresponding to a date. |
79
|
|
|
* The year is returned as an integer in the range 1900-9999. |
80
|
|
|
* |
81
|
|
|
* Excel Function: |
82
|
|
|
* YEAR(dateValue) |
83
|
|
|
* |
84
|
|
|
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
85
|
|
|
* PHP DateTime object, or a standard date string |
86
|
|
|
* |
87
|
|
|
* @return int|string Year |
88
|
|
|
*/ |
89
|
177 |
|
public static function year($dateValue) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
177 |
|
$dateValue = Helpers::getDateValue($dateValue); |
93
|
2 |
|
} catch (Exception $e) { |
94
|
2 |
|
return $e->getMessage(); |
95
|
|
|
} |
96
|
|
|
|
97
|
175 |
|
if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) { |
98
|
1 |
|
return 1900; |
99
|
|
|
} |
100
|
|
|
// Execute function |
101
|
174 |
|
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue); |
102
|
|
|
|
103
|
174 |
|
return (int) $PHPDateObject->format('Y'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer), |
108
|
|
|
* PHP DateTime object, or a standard date string |
109
|
|
|
*/ |
110
|
106 |
|
private static function weirdCondition($dateValue): int |
111
|
|
|
{ |
112
|
|
|
// Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR) |
113
|
106 |
|
if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) { |
114
|
93 |
|
if (is_bool($dateValue)) { |
115
|
2 |
|
return (int) $dateValue; |
116
|
|
|
} |
117
|
91 |
|
if ($dateValue === null) { |
118
|
1 |
|
return 0; |
119
|
|
|
} |
120
|
90 |
|
if (is_numeric($dateValue) && $dateValue < 1 && $dateValue >= 0) { |
121
|
2 |
|
return 0; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
101 |
|
return -1; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|