Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Datetime;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Defines Gregorian Calendar date values.
|
Chris@0
|
7 *
|
Chris@0
|
8 * Lots of helpful functions for use in massaging dates, specific to the
|
Chris@0
|
9 * Gregorian calendar system. The values include both translated and
|
Chris@0
|
10 * untranslated values.
|
Chris@0
|
11 *
|
Chris@0
|
12 * Untranslated values are useful as array keys and as css identifiers, and
|
Chris@0
|
13 * should be listed in English.
|
Chris@0
|
14 *
|
Chris@0
|
15 * Translated values are useful for display to the user. All values that need
|
Chris@0
|
16 * translation should be hard-coded and wrapped in t() so the translation system
|
Chris@0
|
17 * will be able to process them.
|
Chris@0
|
18 */
|
Chris@0
|
19 class DateHelper {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Constructs an untranslated array of month names.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @return array
|
Chris@0
|
25 * An array of month names.
|
Chris@0
|
26 */
|
Chris@0
|
27 public static function monthNamesUntranslated() {
|
Chris@0
|
28 // Force the key to use the correct month value, rather than
|
Chris@0
|
29 // starting with zero.
|
Chris@0
|
30 return [
|
Chris@0
|
31 1 => 'January',
|
Chris@0
|
32 2 => 'February',
|
Chris@0
|
33 3 => 'March',
|
Chris@0
|
34 4 => 'April',
|
Chris@0
|
35 5 => 'May',
|
Chris@0
|
36 6 => 'June',
|
Chris@0
|
37 7 => 'July',
|
Chris@0
|
38 8 => 'August',
|
Chris@0
|
39 9 => 'September',
|
Chris@0
|
40 10 => 'October',
|
Chris@0
|
41 11 => 'November',
|
Chris@0
|
42 12 => 'December',
|
Chris@0
|
43 ];
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Constructs an untranslated array of abbreviated month names.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @return array
|
Chris@0
|
50 * An array of month names.
|
Chris@0
|
51 */
|
Chris@0
|
52 public static function monthNamesAbbrUntranslated() {
|
Chris@0
|
53 // Force the key to use the correct month value, rather than
|
Chris@0
|
54 // starting with zero.
|
Chris@0
|
55 return [
|
Chris@0
|
56 1 => 'Jan',
|
Chris@0
|
57 2 => 'Feb',
|
Chris@0
|
58 3 => 'Mar',
|
Chris@0
|
59 4 => 'Apr',
|
Chris@0
|
60 5 => 'May',
|
Chris@0
|
61 6 => 'Jun',
|
Chris@0
|
62 7 => 'Jul',
|
Chris@0
|
63 8 => 'Aug',
|
Chris@0
|
64 9 => 'Sep',
|
Chris@0
|
65 10 => 'Oct',
|
Chris@0
|
66 11 => 'Nov',
|
Chris@0
|
67 12 => 'Dec',
|
Chris@0
|
68 ];
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Returns a translated array of month names.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param bool $required
|
Chris@0
|
75 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
76 * Defaults to FALSE.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @return array
|
Chris@0
|
79 * An array of month names.
|
Chris@0
|
80 */
|
Chris@0
|
81 public static function monthNames($required = FALSE) {
|
Chris@0
|
82 // Force the key to use the correct month value, rather than
|
Chris@0
|
83 // starting with zero.
|
Chris@0
|
84 $monthnames = [
|
Chris@0
|
85 1 => t('January', [], ['context' => 'Long month name']),
|
Chris@0
|
86 2 => t('February', [], ['context' => 'Long month name']),
|
Chris@0
|
87 3 => t('March', [], ['context' => 'Long month name']),
|
Chris@0
|
88 4 => t('April', [], ['context' => 'Long month name']),
|
Chris@0
|
89 5 => t('May', [], ['context' => 'Long month name']),
|
Chris@0
|
90 6 => t('June', [], ['context' => 'Long month name']),
|
Chris@0
|
91 7 => t('July', [], ['context' => 'Long month name']),
|
Chris@0
|
92 8 => t('August', [], ['context' => 'Long month name']),
|
Chris@0
|
93 9 => t('September', [], ['context' => 'Long month name']),
|
Chris@0
|
94 10 => t('October', [], ['context' => 'Long month name']),
|
Chris@0
|
95 11 => t('November', [], ['context' => 'Long month name']),
|
Chris@0
|
96 12 => t('December', [], ['context' => 'Long month name']),
|
Chris@0
|
97 ];
|
Chris@0
|
98 $none = ['' => ''];
|
Chris@0
|
99 return !$required ? $none + $monthnames : $monthnames;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * Constructs a translated array of month name abbreviations
|
Chris@0
|
104 *
|
Chris@0
|
105 * @param bool $required
|
Chris@0
|
106 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
107 * Defaults to FALSE.
|
Chris@0
|
108 *
|
Chris@0
|
109 * @return array
|
Chris@0
|
110 * An array of month abbreviations.
|
Chris@0
|
111 */
|
Chris@0
|
112 public static function monthNamesAbbr($required = FALSE) {
|
Chris@0
|
113 // Force the key to use the correct month value, rather than
|
Chris@0
|
114 // starting with zero.
|
Chris@0
|
115 $monthnames = [
|
Chris@0
|
116 1 => t('Jan', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
117 2 => t('Feb', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
118 3 => t('Mar', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
119 4 => t('Apr', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
120 5 => t('May', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
121 6 => t('Jun', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
122 7 => t('Jul', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
123 8 => t('Aug', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
124 9 => t('Sep', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
125 10 => t('Oct', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
126 11 => t('Nov', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
127 12 => t('Dec', [], ['context' => 'Abbreviated month name']),
|
Chris@0
|
128 ];
|
Chris@0
|
129 $none = ['' => ''];
|
Chris@0
|
130 return !$required ? $none + $monthnames : $monthnames;
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * Constructs an untranslated array of week days.
|
Chris@0
|
135 *
|
Chris@0
|
136 * @return array
|
Chris@0
|
137 * An array of week day names
|
Chris@0
|
138 */
|
Chris@0
|
139 public static function weekDaysUntranslated() {
|
Chris@0
|
140 return [
|
Chris@0
|
141 'Sunday',
|
Chris@0
|
142 'Monday',
|
Chris@0
|
143 'Tuesday',
|
Chris@0
|
144 'Wednesday',
|
Chris@0
|
145 'Thursday',
|
Chris@0
|
146 'Friday',
|
Chris@0
|
147 'Saturday',
|
Chris@0
|
148 ];
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * Returns a translated array of week names.
|
Chris@0
|
153 *
|
Chris@0
|
154 * @param bool $required
|
Chris@0
|
155 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
156 * Defaults to FALSE.
|
Chris@0
|
157 *
|
Chris@0
|
158 * @return array
|
Chris@0
|
159 * An array of week day names
|
Chris@0
|
160 */
|
Chris@0
|
161 public static function weekDays($required = FALSE) {
|
Chris@0
|
162 $weekdays = [
|
Chris@0
|
163 t('Sunday'),
|
Chris@0
|
164 t('Monday'),
|
Chris@0
|
165 t('Tuesday'),
|
Chris@0
|
166 t('Wednesday'),
|
Chris@0
|
167 t('Thursday'),
|
Chris@0
|
168 t('Friday'),
|
Chris@0
|
169 t('Saturday'),
|
Chris@0
|
170 ];
|
Chris@0
|
171 $none = ['' => ''];
|
Chris@0
|
172 return !$required ? $none + $weekdays : $weekdays;
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * Constructs a translated array of week day abbreviations.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @param bool $required
|
Chris@0
|
179 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
180 * Defaults to FALSE.
|
Chris@0
|
181 *
|
Chris@0
|
182 * @return array
|
Chris@0
|
183 * An array of week day abbreviations
|
Chris@0
|
184 */
|
Chris@0
|
185 public static function weekDaysAbbr($required = FALSE) {
|
Chris@0
|
186 $weekdays = [
|
Chris@0
|
187 t('Sun', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
188 t('Mon', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
189 t('Tue', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
190 t('Wed', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
191 t('Thu', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
192 t('Fri', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
193 t('Sat', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
194 ];
|
Chris@0
|
195 $none = ['' => ''];
|
Chris@0
|
196 return !$required ? $none + $weekdays : $weekdays;
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 * Constructs a translated array of 2-letter week day abbreviations.
|
Chris@0
|
201 *
|
Chris@0
|
202 * @param bool $required
|
Chris@0
|
203 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
204 * Defaults to FALSE.
|
Chris@0
|
205 *
|
Chris@0
|
206 * @return array
|
Chris@0
|
207 * An array of week day 2 letter abbreviations
|
Chris@0
|
208 */
|
Chris@0
|
209 public static function weekDaysAbbr2($required = FALSE) {
|
Chris@0
|
210 $weekdays = [
|
Chris@0
|
211 t('Su', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
212 t('Mo', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
213 t('Tu', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
214 t('We', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
215 t('Th', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
216 t('Fr', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
217 t('Sa', [], ['context' => 'Abbreviated weekday']),
|
Chris@0
|
218 ];
|
Chris@0
|
219 $none = ['' => ''];
|
Chris@0
|
220 return !$required ? $none + $weekdays : $weekdays;
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 /**
|
Chris@0
|
224 * Constructs a translated array of 1-letter week day abbreviations.
|
Chris@0
|
225 *
|
Chris@0
|
226 * @param bool $required
|
Chris@0
|
227 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
228 * Defaults to FALSE.
|
Chris@0
|
229 *
|
Chris@0
|
230 * @return array
|
Chris@0
|
231 * An array of week day 1 letter abbreviations
|
Chris@0
|
232 */
|
Chris@0
|
233 public static function weekDaysAbbr1($required = FALSE) {
|
Chris@0
|
234 $weekdays = [
|
Chris@0
|
235 t('S', [], ['context' => 'Abbreviated 1 letter weekday Sunday']),
|
Chris@0
|
236 t('M', [], ['context' => 'Abbreviated 1 letter weekday Monday']),
|
Chris@0
|
237 t('T', [], ['context' => 'Abbreviated 1 letter weekday Tuesday']),
|
Chris@0
|
238 t('W', [], ['context' => 'Abbreviated 1 letter weekday Wednesday']),
|
Chris@0
|
239 t('T', [], ['context' => 'Abbreviated 1 letter weekday Thursday']),
|
Chris@0
|
240 t('F', [], ['context' => 'Abbreviated 1 letter weekday Friday']),
|
Chris@0
|
241 t('S', [], ['context' => 'Abbreviated 1 letter weekday Saturday']),
|
Chris@0
|
242 ];
|
Chris@0
|
243 $none = ['' => ''];
|
Chris@0
|
244 return !$required ? $none + $weekdays : $weekdays;
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * Reorders weekdays to match the first day of the week.
|
Chris@0
|
249 *
|
Chris@0
|
250 * @param array $weekdays
|
Chris@0
|
251 * An array of weekdays.
|
Chris@0
|
252 *
|
Chris@0
|
253 * @return array
|
Chris@0
|
254 * An array of weekdays reordered to match the first day of the week. The
|
Chris@0
|
255 * keys will remain unchanged. For example, if the first day of the week is
|
Chris@0
|
256 * set to be Monday, the array keys will be [1, 2, 3, 4, 5, 6, 0].
|
Chris@0
|
257 */
|
Chris@0
|
258 public static function weekDaysOrdered($weekdays) {
|
Chris@0
|
259 $first_day = \Drupal::config('system.date')->get('first_day');
|
Chris@0
|
260 if ($first_day > 0) {
|
Chris@0
|
261 for ($i = 1; $i <= $first_day; $i++) {
|
Chris@0
|
262 // Reset the array to the first element.
|
Chris@0
|
263 reset($weekdays);
|
Chris@0
|
264 // Retrieve the first week day value.
|
Chris@0
|
265 $last = current($weekdays);
|
Chris@0
|
266 // Store the corresponding key.
|
Chris@0
|
267 $key = key($weekdays);
|
Chris@0
|
268 // Remove this week day from the beginning of the array.
|
Chris@0
|
269 unset($weekdays[$key]);
|
Chris@0
|
270 // Add this week day to the end of the array.
|
Chris@0
|
271 $weekdays[$key] = $last;
|
Chris@0
|
272 }
|
Chris@0
|
273 }
|
Chris@0
|
274 return $weekdays;
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 /**
|
Chris@0
|
278 * Constructs an array of years in a specified range.
|
Chris@0
|
279 *
|
Chris@0
|
280 * @param int $min
|
Chris@0
|
281 * (optional) The minimum year in the array. Defaults to zero.
|
Chris@0
|
282 * @param int $max
|
Chris@0
|
283 * (optional) The maximum year in the array. Defaults to zero.
|
Chris@0
|
284 * @param bool $required
|
Chris@0
|
285 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
286 * Defaults to FALSE.
|
Chris@0
|
287 *
|
Chris@0
|
288 * @return array
|
Chris@0
|
289 * An array of years in the selected range.
|
Chris@0
|
290 */
|
Chris@0
|
291 public static function years($min = 0, $max = 0, $required = FALSE) {
|
Chris@0
|
292 // Ensure $min and $max are valid values.
|
Chris@0
|
293 if (empty($min)) {
|
Chris@0
|
294 $min = intval(date('Y', REQUEST_TIME) - 3);
|
Chris@0
|
295 }
|
Chris@0
|
296 if (empty($max)) {
|
Chris@0
|
297 $max = intval(date('Y', REQUEST_TIME) + 3);
|
Chris@0
|
298 }
|
Chris@0
|
299 $none = ['' => ''];
|
Chris@0
|
300 $range = range($min, $max);
|
Chris@0
|
301 $range = array_combine($range, $range);
|
Chris@0
|
302 return !$required ? $none + $range : $range;
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 /**
|
Chris@0
|
306 * Constructs an array of days in a month.
|
Chris@0
|
307 *
|
Chris@0
|
308 * @param bool $required
|
Chris@0
|
309 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
310 * Defaults to FALSE.
|
Chris@0
|
311 * @param int $month
|
Chris@0
|
312 * (optional) The month in which to find the number of days. Defaults to
|
Chris@0
|
313 * NULL.
|
Chris@0
|
314 * @param int $year
|
Chris@0
|
315 * (optional) The year in which to find the number of days. Defaults to
|
Chris@0
|
316 * NULL.
|
Chris@0
|
317 *
|
Chris@0
|
318 * @return array
|
Chris@0
|
319 * An array of days for the selected month.
|
Chris@0
|
320 */
|
Chris@0
|
321 public static function days($required = FALSE, $month = NULL, $year = NULL) {
|
Chris@0
|
322 // If we have a month and year, find the right last day of the month.
|
Chris@0
|
323 if (!empty($month) && !empty($year)) {
|
Chris@0
|
324 $date = new DrupalDateTime($year . '-' . $month . '-01 00:00:00', 'UTC');
|
Chris@0
|
325 $max = $date->format('t');
|
Chris@0
|
326 }
|
Chris@0
|
327 // If there is no month and year given, default to 31.
|
Chris@0
|
328 if (empty($max)) {
|
Chris@0
|
329 $max = 31;
|
Chris@0
|
330 }
|
Chris@0
|
331 $none = ['' => ''];
|
Chris@0
|
332 $range = range(1, $max);
|
Chris@0
|
333 $range = array_combine($range, $range);
|
Chris@0
|
334 return !$required ? $none + $range : $range;
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 /**
|
Chris@0
|
338 * Constructs an array of hours.
|
Chris@0
|
339 *
|
Chris@0
|
340 * @param string $format
|
Chris@0
|
341 * (optional) A date format string that indicates the format to use for the
|
Chris@0
|
342 * hours. Defaults to 'H'.
|
Chris@0
|
343 * @param bool $required
|
Chris@0
|
344 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
345 * Defaults to FALSE.
|
Chris@0
|
346 *
|
Chris@0
|
347 * @return array
|
Chris@0
|
348 * An array of hours in the selected format.
|
Chris@0
|
349 */
|
Chris@0
|
350 public static function hours($format = 'H', $required = FALSE) {
|
Chris@0
|
351 $hours = [];
|
Chris@0
|
352 if ($format == 'h' || $format == 'g') {
|
Chris@0
|
353 $min = 1;
|
Chris@0
|
354 $max = 12;
|
Chris@0
|
355 }
|
Chris@0
|
356 else {
|
Chris@0
|
357 $min = 0;
|
Chris@0
|
358 $max = 23;
|
Chris@0
|
359 }
|
Chris@0
|
360 for ($i = $min; $i <= $max; $i++) {
|
Chris@0
|
361 $formatted = ($format == 'H' || $format == 'h') ? DrupalDateTime::datePad($i) : $i;
|
Chris@0
|
362 $hours[$i] = $formatted;
|
Chris@0
|
363 }
|
Chris@0
|
364 $none = ['' => ''];
|
Chris@0
|
365 return !$required ? $none + $hours : $hours;
|
Chris@0
|
366 }
|
Chris@0
|
367
|
Chris@0
|
368 /**
|
Chris@0
|
369 * Constructs an array of minutes.
|
Chris@0
|
370 *
|
Chris@0
|
371 * @param string $format
|
Chris@0
|
372 * (optional) A date format string that indicates the format to use for the
|
Chris@0
|
373 * minutes. Defaults to 'i'.
|
Chris@0
|
374 * @param bool $required
|
Chris@0
|
375 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
376 * Defaults to FALSE.
|
Chris@0
|
377 * @param int $increment
|
Chris@0
|
378 * An integer value to increment the values. Defaults to 1.
|
Chris@0
|
379 *
|
Chris@0
|
380 * @return array
|
Chris@0
|
381 * An array of minutes in the selected format.
|
Chris@0
|
382 */
|
Chris@0
|
383 public static function minutes($format = 'i', $required = FALSE, $increment = 1) {
|
Chris@0
|
384 $minutes = [];
|
Chris@0
|
385 // Ensure $increment has a value so we don't loop endlessly.
|
Chris@0
|
386 if (empty($increment)) {
|
Chris@0
|
387 $increment = 1;
|
Chris@0
|
388 }
|
Chris@0
|
389 for ($i = 0; $i < 60; $i += $increment) {
|
Chris@0
|
390 $formatted = $format == 'i' ? DrupalDateTime::datePad($i) : $i;
|
Chris@0
|
391 $minutes[$i] = $formatted;
|
Chris@0
|
392 }
|
Chris@0
|
393 $none = ['' => ''];
|
Chris@0
|
394 return !$required ? $none + $minutes : $minutes;
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * Constructs an array of seconds.
|
Chris@0
|
399 *
|
Chris@0
|
400 * @param string $format
|
Chris@0
|
401 * (optional) A date format string that indicates the format to use for the
|
Chris@0
|
402 * seconds. Defaults to 's'.
|
Chris@0
|
403 * @param bool $required
|
Chris@0
|
404 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
405 * Defaults to FALSE.
|
Chris@0
|
406 * @param int $increment
|
Chris@0
|
407 * An integer value to increment the values. Defaults to 1.
|
Chris@0
|
408 *
|
Chris@0
|
409 * @return array
|
Chris@0
|
410 * An array of seconds in the selected format.
|
Chris@0
|
411 */
|
Chris@0
|
412 public static function seconds($format = 's', $required = FALSE, $increment = 1) {
|
Chris@0
|
413 $seconds = [];
|
Chris@0
|
414 // Ensure $increment has a value so we don't loop endlessly.
|
Chris@0
|
415 if (empty($increment)) {
|
Chris@0
|
416 $increment = 1;
|
Chris@0
|
417 }
|
Chris@0
|
418 for ($i = 0; $i < 60; $i += $increment) {
|
Chris@0
|
419 $formatted = $format == 's' ? DrupalDateTime::datePad($i) : $i;
|
Chris@0
|
420 $seconds[$i] = $formatted;
|
Chris@0
|
421 }
|
Chris@0
|
422 $none = ['' => ''];
|
Chris@0
|
423 return !$required ? $none + $seconds : $seconds;
|
Chris@0
|
424 }
|
Chris@0
|
425
|
Chris@0
|
426 /**
|
Chris@0
|
427 * Constructs an array of AM and PM options.
|
Chris@0
|
428 *
|
Chris@0
|
429 * @param bool $required
|
Chris@0
|
430 * (optional) If FALSE, the returned array will include a blank value.
|
Chris@0
|
431 * Defaults to FALSE.
|
Chris@0
|
432 *
|
Chris@0
|
433 * @return array
|
Chris@0
|
434 * An array of AM and PM options.
|
Chris@0
|
435 */
|
Chris@0
|
436 public static function ampm($required = FALSE) {
|
Chris@0
|
437 $none = ['' => ''];
|
Chris@0
|
438 $ampm = [
|
Chris@0
|
439 'am' => t('am', [], ['context' => 'ampm']),
|
Chris@0
|
440 'pm' => t('pm', [], ['context' => 'ampm']),
|
Chris@0
|
441 ];
|
Chris@0
|
442 return !$required ? $none + $ampm : $ampm;
|
Chris@0
|
443 }
|
Chris@0
|
444
|
Chris@0
|
445 /**
|
Chris@0
|
446 * Identifies the number of days in a month for a date.
|
Chris@0
|
447 *
|
Chris@0
|
448 * @param mixed $date
|
Chris@0
|
449 * (optional) A DrupalDateTime object or a date string.
|
Chris@0
|
450 * Defaults to NULL, which means to use the current date.
|
Chris@0
|
451 *
|
Chris@0
|
452 * @return int
|
Chris@0
|
453 * The number of days in the month.
|
Chris@0
|
454 */
|
Chris@0
|
455 public static function daysInMonth($date = NULL) {
|
Chris@0
|
456 if (!$date instanceof DrupalDateTime) {
|
Chris@0
|
457 $date = new DrupalDateTime($date);
|
Chris@0
|
458 }
|
Chris@0
|
459 if (!$date->hasErrors()) {
|
Chris@0
|
460 return $date->format('t');
|
Chris@0
|
461 }
|
Chris@0
|
462 return NULL;
|
Chris@0
|
463 }
|
Chris@0
|
464
|
Chris@0
|
465 /**
|
Chris@0
|
466 * Identifies the number of days in a year for a date.
|
Chris@0
|
467 *
|
Chris@0
|
468 * @param mixed $date
|
Chris@0
|
469 * (optional) A DrupalDateTime object or a date string.
|
Chris@0
|
470 * Defaults to NULL, which means to use the current date.
|
Chris@0
|
471 *
|
Chris@0
|
472 * @return int
|
Chris@0
|
473 * The number of days in the year.
|
Chris@0
|
474 */
|
Chris@0
|
475 public static function daysInYear($date = NULL) {
|
Chris@0
|
476 if (!$date instanceof DrupalDateTime) {
|
Chris@0
|
477 $date = new DrupalDateTime($date);
|
Chris@0
|
478 }
|
Chris@0
|
479 if (!$date->hasErrors()) {
|
Chris@0
|
480 if ($date->format('L')) {
|
Chris@0
|
481 return 366;
|
Chris@0
|
482 }
|
Chris@0
|
483 else {
|
Chris@0
|
484 return 365;
|
Chris@0
|
485 }
|
Chris@0
|
486 }
|
Chris@0
|
487 return NULL;
|
Chris@0
|
488 }
|
Chris@0
|
489
|
Chris@0
|
490 /**
|
Chris@0
|
491 * Returns day of week for a given date (0 = Sunday).
|
Chris@0
|
492 *
|
Chris@0
|
493 * @param mixed $date
|
Chris@0
|
494 * (optional) A DrupalDateTime object or a date string.
|
Chris@0
|
495 * Defaults to NULL, which means use the current date.
|
Chris@0
|
496 *
|
Chris@0
|
497 * @return int
|
Chris@0
|
498 * The number of the day in the week.
|
Chris@0
|
499 */
|
Chris@0
|
500 public static function dayOfWeek($date = NULL) {
|
Chris@0
|
501 if (!$date instanceof DrupalDateTime) {
|
Chris@0
|
502 $date = new DrupalDateTime($date);
|
Chris@0
|
503 }
|
Chris@0
|
504 if (!$date->hasErrors()) {
|
Chris@0
|
505 return $date->format('w');
|
Chris@0
|
506 }
|
Chris@0
|
507 return NULL;
|
Chris@0
|
508 }
|
Chris@0
|
509
|
Chris@0
|
510 /**
|
Chris@0
|
511 * Returns translated name of the day of week for a given date.
|
Chris@0
|
512 *
|
Chris@0
|
513 * @param mixed $date
|
Chris@0
|
514 * (optional) A DrupalDateTime object or a date string.
|
Chris@0
|
515 * Defaults to NULL, which means use the current date.
|
Chris@0
|
516 * @param string $abbr
|
Chris@0
|
517 * (optional) Whether to return the abbreviated name for that day.
|
Chris@0
|
518 * Defaults to TRUE.
|
Chris@0
|
519 *
|
Chris@0
|
520 * @return string
|
Chris@0
|
521 * The name of the day in the week for that date.
|
Chris@0
|
522 */
|
Chris@0
|
523 public static function dayOfWeekName($date = NULL, $abbr = TRUE) {
|
Chris@0
|
524 if (!$date instanceof DrupalDateTime) {
|
Chris@0
|
525 $date = new DrupalDateTime($date);
|
Chris@0
|
526 }
|
Chris@0
|
527 $dow = self::dayOfWeek($date);
|
Chris@0
|
528 $days = $abbr ? self::weekDaysAbbr() : self::weekDays();
|
Chris@0
|
529 return $days[$dow];
|
Chris@0
|
530 }
|
Chris@0
|
531
|
Chris@0
|
532 }
|