1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AppUtils; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
|
9
|
|
|
class ConvertHelper_Date |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array<int,string[]> |
13
|
|
|
*/ |
14
|
|
|
protected static $months = array(); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string[] |
18
|
|
|
*/ |
19
|
|
|
protected static $days = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
protected static $daysShort = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string[] |
28
|
|
|
*/ |
29
|
|
|
protected static $daysInvariant = array( |
30
|
|
|
'Monday', |
31
|
|
|
'Tuesday', |
32
|
|
|
'Wednesday', |
33
|
|
|
'Thursday', |
34
|
|
|
'Friday', |
35
|
|
|
'Saturday', |
36
|
|
|
'Sunday' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Converts a date to the corresponding day name. |
41
|
|
|
* |
42
|
|
|
* @param DateTime $date |
43
|
|
|
* @param bool $short |
44
|
|
|
* @return string|NULL |
45
|
|
|
*/ |
46
|
|
|
public static function toDayName(DateTime $date, bool $short=false) : ?string |
47
|
|
|
{ |
48
|
|
|
$day = $date->format('l'); |
49
|
|
|
$invariant = self::getDayNamesInvariant(); |
50
|
|
|
|
51
|
|
|
$idx = array_search($day, $invariant); |
52
|
|
|
if($idx !== false) { |
53
|
|
|
$localized = self::getDayNames($short); |
54
|
|
|
return $localized[$idx]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieves a list of english day names. |
62
|
|
|
* @return string[] |
63
|
|
|
*/ |
64
|
|
|
public static function getDayNamesInvariant() : array |
65
|
|
|
{ |
66
|
|
|
return self::$daysInvariant; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Retrieves the day names list for the current locale. |
71
|
|
|
* |
72
|
|
|
* @param bool $short |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
|
|
public static function getDayNames(bool $short=false) : array |
76
|
|
|
{ |
77
|
|
|
self::initDays(); |
78
|
|
|
|
79
|
|
|
if($short) { |
80
|
|
|
return self::$daysShort; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return self::$days; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Transforms a date into a generic human-readable date, optionally with time. |
88
|
|
|
* If the year is the same as the current one, it is omitted. |
89
|
|
|
* |
90
|
|
|
* - 6 Jan 2012 |
91
|
|
|
* - 12 Dec 2012 17:45 |
92
|
|
|
* - 5 Aug |
93
|
|
|
* |
94
|
|
|
* @param DateTime $date |
95
|
|
|
* @param bool $includeTime |
96
|
|
|
* @param bool $shortMonth |
97
|
|
|
* @return string |
98
|
|
|
* |
99
|
|
|
* @throws ConvertHelper_Exception |
100
|
|
|
* @see ConvertHelper::ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER |
101
|
|
|
*/ |
102
|
|
|
public static function toListLabel(DateTime $date, bool $includeTime = false, bool $shortMonth = false) : string |
103
|
|
|
{ |
104
|
|
|
$today = new DateTime(); |
105
|
|
|
if($date->format('d.m.Y') === $today->format('d.m.Y')) |
106
|
|
|
{ |
107
|
|
|
$label = t('Today'); |
108
|
|
|
} |
109
|
|
|
else |
110
|
|
|
{ |
111
|
|
|
$label = $date->format('d') . '. ' . self::month2string((int)$date->format('m'), $shortMonth) . ' '; |
112
|
|
|
|
113
|
|
|
if ($date->format('Y') != date('Y')) |
114
|
|
|
{ |
115
|
|
|
$label .= $date->format('Y'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$toolTipDateFormat = 'd.m.Y'; |
120
|
|
|
|
121
|
|
|
if ($includeTime) |
122
|
|
|
{ |
123
|
|
|
$label .= $date->format(' H:i'); |
124
|
|
|
$toolTipDateFormat .= ' H:i'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return |
128
|
|
|
'<span title="'.$date->format($toolTipDateFormat).'">'. |
129
|
|
|
trim($label). |
130
|
|
|
'</span>'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns a human-readable month name given the month number. Can optionally |
135
|
|
|
* return the shorthand version of the month. Translated into the current |
136
|
|
|
* application locale. |
137
|
|
|
* |
138
|
|
|
* @param int|string $monthNr |
139
|
|
|
* @param boolean $short |
140
|
|
|
* @return string |
141
|
|
|
* |
142
|
|
|
* @throws ConvertHelper_Exception |
143
|
|
|
* @see ConvertHelper::ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER |
144
|
|
|
*/ |
145
|
|
|
public static function month2string($monthNr, bool $short = false) : string |
146
|
|
|
{ |
147
|
|
|
self::initMonths(); |
148
|
|
|
|
149
|
|
|
$monthNr = intval($monthNr); |
150
|
|
|
if (!isset(self::$months[$monthNr])) |
151
|
|
|
{ |
152
|
|
|
throw new ConvertHelper_Exception( |
153
|
|
|
'Invalid month number', |
154
|
|
|
sprintf('%1$s is not a valid month number.', $monthNr), |
155
|
|
|
ConvertHelper::ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($short) { |
160
|
|
|
return self::$months[$monthNr][1]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return self::$months[$monthNr][0]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Converts a DateTime object to a timestamp, which |
168
|
|
|
* is PHP 5.2 compatible. |
169
|
|
|
* |
170
|
|
|
* @param DateTime $date |
171
|
|
|
* @return integer |
172
|
|
|
*/ |
173
|
|
|
public static function toTimestamp(DateTime $date) : int |
174
|
|
|
{ |
175
|
|
|
return (int)$date->format('U'); |
176
|
|
|
} |
177
|
|
|
/** |
178
|
|
|
* Converts a timestamp into a DateTime instance. |
179
|
|
|
* |
180
|
|
|
* @param int $timestamp |
181
|
|
|
* @return DateTime |
182
|
|
|
*/ |
183
|
|
|
public static function fromTimestamp(int $timestamp) : DateTime |
184
|
|
|
{ |
185
|
|
|
$date = new DateTime(); |
186
|
|
|
$date->setTimestamp($timestamp); |
187
|
|
|
return $date; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
private static function initMonths() : void |
192
|
|
|
{ |
193
|
|
|
if (!empty(self::$months)) |
194
|
|
|
{ |
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
self::$months = array( |
199
|
|
|
1 => array(t('January'), t('Jan')), |
200
|
|
|
2 => array(t('February'), t('Feb')), |
201
|
|
|
3 => array(t('March'), t('Mar')), |
202
|
|
|
4 => array(t('April'), t('Apr')), |
203
|
|
|
5 => array(t('May'), t('May')), |
204
|
|
|
6 => array(t('June'), t('Jun')), |
205
|
|
|
7 => array(t('July'), t('Jul')), |
206
|
|
|
8 => array(t('August'), t('Aug')), |
207
|
|
|
9 => array(t('September'), t('Sep')), |
208
|
|
|
10 => array(t('October'), t('Oct')), |
209
|
|
|
11 => array(t('November'), t('Nov')), |
210
|
|
|
12 => array(t('December'), t('Dec')) |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
private static function initDays() : void |
215
|
|
|
{ |
216
|
|
|
if(!empty(self::$daysShort)) |
217
|
|
|
{ |
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
self::$daysShort = array( |
222
|
|
|
t('Mon'), |
223
|
|
|
t('Tue'), |
224
|
|
|
t('Wed'), |
225
|
|
|
t('Thu'), |
226
|
|
|
t('Fri'), |
227
|
|
|
t('Sat'), |
228
|
|
|
t('Sun') |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
self::$days = array( |
232
|
|
|
t('Monday'), |
233
|
|
|
t('Tuesday'), |
234
|
|
|
t('Wednesday'), |
235
|
|
|
t('Thursday'), |
236
|
|
|
t('Friday'), |
237
|
|
|
t('Saturday'), |
238
|
|
|
t('Sunday') |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|