/
class.datetimeobj.php
582 lines (523 loc) · 19.2 KB
/
class.datetimeobj.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/**
* @package core
*/
/**
* The DateTimeObj provides static functions regarding dates in Symphony.
* Symphony will set the default timezone of the system using the value from
* the Configuration values. Alternatively a new settings can be set using the
* `setSettings` function. Symphony parses all input dates against the Configuration
* date formats by default for better support with non English dates.
*/
class DateTimeObj
{
/**
* Holds the various settings for the formats that the `DateTimeObj` should
* use when parsing input dates.
*
* @since Symphony 2.7.0 it contains default values to prevent the case where
* no settings are set
* @since Symphony 2.2.4
* @var array
*/
private static $settings = array(
'time_format' => 'g:i a',
'date_format' => 'm/d/Y',
);
/**
* Mapping PHP to Moment date formats.
*/
protected static $date_mappings = array(
'Y/m/d' => 'YYYY/MM/DD', // e. g. 2014/01/02
'd/m/Y' => 'DD/MM/YYYY', // e. g. 01/02/2014
'm/d/Y' => 'MM/DD/YYYY', // e. g. 01/02/2014
'm/d/y' => 'MM/DD/YY', // e. g. 01/02/14
'Y-m-d' => 'YYYY-MM-DD', // e. g. 2014-01-02
'm-d-Y' => 'MM-DD-YYYY', // e. g. 01-02-2014
'm-d-y' => 'MM-DD-YY', // e. g. 01-02-14
'd.m.Y' => 'DD.MM.YYYY', // e. g. 02.01.2014
'j.n.Y' => 'D.M.YYYY', // e. g. 2.1.2014 - no leading zeros
'j.n.y' => 'D.M.YY', // e. g. 2.1.14 - no leading zeros
'd.m.Y' => 'DD.MM.YYYY', // e. g. 02.01.2014
'd.m.y' => 'DD.MM.YYYY', // e. g. 02.01.14
'd F Y' => 'DD MMMM YYYY', // e. g. 02 January 2014
'd. F Y' => 'DD. MMMM YYYY', // e. g. 02. January 2014
'd M Y' => 'DD MMM YYYY', // e. g. 02 Jan 2014
'd. M Y' => 'DD. MMM. YYYY', // e. g. 02. Jan 2014
'j. F Y' => 'D. MMMM YYYY', // e. g. 2. January 2014 - no leading zeros
'j. M. Y' => 'D. MMM. YYYY', // e. g. 2. Jan. 2014 - no leading zeros
);
/**
* Mapping PHP to Moment time formats.
*/
protected static $time_mappings = array(
'H:i:s' => 'HH:mm:ss', // e. g. 20:45:32
'H:i' => 'HH:mm', // e. g. 20:45
'g:i:s a' => 'h:mm:ss a', // e. g. 8:45:32 pm
'g:i a' => 'h:mm a', // e. g. 8:45 pm
);
/**
* This function takes an array of settings for `DateTimeObj` to use when parsing
* input dates. The following settings are supported, `time_format`, `date_format`,
* `datetime_separator` and `timezone`. This equates to Symphony's default `region`
* group set in the `Configuration` class. If any of these values are not provided
* the class will fallback to existing `self::$settings` values
*
* @since Symphony 2.2.4
* @param array $settings
* An associative array of formats for this class to use to format
* dates
*/
public static function setSettings(array $settings = array())
{
// Date format
if (isset($settings['date_format'])) {
self::$settings['date_format'] = $settings['date_format'];
}
// Time format
if (isset($settings['time_format'])) {
self::$settings['time_format'] = $settings['time_format'];
}
// Datetime separator
if (isset($settings['datetime_separator'])) {
self::$settings['datetime_separator'] = $settings['datetime_separator'];
} elseif (!isset(self::$settings['datetime_separator'])) {
self::$settings['datetime_separator'] = ' ';
}
// Datetime format
if (isset($settings['datetime_format'])) {
self::$settings['datetime_format'] = $settings['datetime_format'];
} elseif (!isset(self::$settings['datetime_format'])) {
self::$settings['datetime_format'] = self::$settings['date_format'] . self::$settings['datetime_separator'] . self::$settings['time_format'];
}
// Timezone
if (isset($settings['timezone']) && !empty($settings['timezone'])) {
self::$settings['timezone'] = $settings['timezone'];
self::setDefaultTimezone($settings['timezone']);
} elseif (!isset(self::$settings['timezone'])) {
self::$settings['timezone'] = ini_get('date.timezone');
if (empty(self::$settings['timezone'])) {
self::$settings['timezone'] = 'UTC';
}
}
}
/**
* Accessor function for the settings of the DateTimeObj. Currently
* the available settings are `time_format`, `date_format`,
* `datetime_format` and `datetime_separator`. If `$name` is not
* provided, the entire `$settings` array is returned.
*
* @since Symphony 2.2.4
* @param string $name
* @return array|string|null
* If `$name` is omitted this function returns array.
* If `$name` is not set, this fucntion returns `null`
* If `$name` is set, this function returns string
*/
public static function getSetting($name = null)
{
if (is_null($name)) {
return self::$settings;
}
if (isset(self::$settings[$name])) {
return self::$settings[$name];
}
return null;
}
/**
* Uses PHP's date_default_timezone_set function to set the system
* timezone. If the timezone provided is invalid, a `E_USER_WARNING` will be
* raised.
*
* @link http://php.net/manual/en/function.date-default-timezone-set.php
* @link http://www.php.net/manual/en/timezones.php
* @param string $timezone
* A valid timezone identifier, such as UTC or Europe/Lisbon
*/
public static function setDefaultTimezone($timezone)
{
if (!@date_default_timezone_set($timezone)) {
trigger_error(__('Invalid timezone %s', array($timezone)), E_USER_WARNING);
}
}
/**
* Validate a given date and time string
*
* @param string $string
* A date and time string or timestamp to validate
* @return boolean
* Returns true for valid dates, otherwise false
*/
public static function validate($string)
{
try {
if (is_numeric($string) && (int)$string == $string) {
$date = new DateTime('@' . $string);
} else {
$date = self::parse($string);
}
} catch (Exception $ex) {
return false;
}
// String is empty or not a valid date
if (empty($string) || $date === false) {
return false;
// String is a valid date
} else {
return true;
}
}
/**
* Given a `$format`, and a `$timestamp`,
* return the date in the format provided. This function is a basic
* wrapper for PHP's DateTime object. If the `$timestamp` is omitted,
* the current timestamp will be used. Optionally, you pass a
* timezone identifier with this function to localise the output
*
* If you like to display a date in the backend, please make use
* of `DateTimeObj::format()` which allows date and time localization
*
* @see class.datetimeobj.php#format()
* @link http://www.php.net/manual/en/book.datetime.php
* @param string $format
* A valid PHP date format
* @param null|string $timestamp (optional)
* A unix timestamp to format. 'now' or omitting this parameter will
* result in the current time being used
* @param string $timezone (optional)
* The timezone associated with the timestamp
* @return string|boolean
* The formatted date, of if the date could not be parsed, false.
*/
public static function get($format, $timestamp = 'now', $timezone = null)
{
return self::format($timestamp, $format, false, $timezone);
}
/**
* Formats the given date and time `$string` based on the given `$format`.
* Optionally the result will be localized and respect a timezone differing
* from the system default. The default output is ISO 8601.
*
* @since Symphony 2.2.1
* @param string $string (optional)
* A string containing date and time, defaults to the current date and time
* @param string $format (optional)
* A valid PHP date format, defaults to ISO 8601
* @param boolean $localize (optional)
* Localizes the output, if true, defaults to true
* @param string $timezone (optional)
* The timezone associated with the timestamp
* @return string|boolean
* The formatted date, or if the date could not be parsed, false.
*/
public static function format($string = 'now', $format = DateTime::ISO8601, $localize = true, $timezone = null)
{
// Parse date
$date = self::parse($string);
if ($date === false) {
return false;
}
// Timezone
// If a timezone was given, apply it
if (!is_null($timezone)) {
$date->setTimezone(new DateTimeZone($timezone));
// No timezone given, apply the default timezone
} elseif (isset(self::$settings['timezone'])) {
$date->setTimezone(new DateTimeZone(self::$settings['timezone']));
}
// Format date
$date = $date->format($format);
// Localize date
// Convert date string from English back to the activated Language
if ($localize === true) {
$date = Lang::localizeDate($date);
}
// Return custom formatted date, use ISO 8601 date by default
return $date;
}
/**
* Parses the given string and returns a DateTime object.
*
* @since Symphony 2.3
* @param string $string (optional)
* A string containing date and time, defaults to the current date and time
* @return DateTime|boolean
* The DateTime object, or if the date could not be parsed, false.
*/
public static function parse($string)
{
// Current date and time
if ($string == 'now' || empty($string)) {
$date = new DateTime();
// Timestamp
} elseif (is_numeric($string)) {
$date = new DateTime('@' . $string);
// Attempt to parse the date provided against the Symphony configuration setting
// in an effort to better support multilingual date formats. Should this fail
// this block will fallback to just passing the date to DateTime constructor,
// which will parse the date assuming it's in an American format.
} else {
// Standardize date
// Convert date string to English
$string = Lang::standardizeDate($string);
// PHP 5.3: Apply Symphony date format using `createFromFormat`
$date = DateTime::createFromFormat(self::$settings['datetime_format'], $string);
if ($date === false) {
$date = DateTime::createFromFormat(self::$settings['date_format'], $string);
}
// Handle dates that are in a different format to Symphony's config
// DateTime is much the same as `strtotime` and will handle relative
// dates.
if ($date === false) {
try {
$date = new DateTime($string);
} catch (Exception $ex) {
// Invalid date, it can't be parsed
return false;
}
}
// If the date is still invalid, just return false.
if ($date === false || $date->format('Y') < 0) {
return false;
}
}
// Return custom formatted date, use ISO 8601 date by default
return $date;
}
/**
* A wrapper for get, this function will force the GMT timezone.
*
* @param string $format
* A valid PHP date format
* @param null|string $timestamp (optional)
* A unix timestamp to format. Omitting this parameter will
* result in the current time being used
* @return string
* The formatted date in GMT
*/
public static function getGMT($format, $timestamp = 'now')
{
return self::format($timestamp, $format, false, 'GMT');
}
/**
* This functions acts as a standard way to get the zones
* available on the system.
*
* @since Symphony 2.3
* @link http://au2.php.net/manual/en/class.datetimezone.php
* @return array
*/
public static function getZones()
{
$ref = new ReflectionClass('DateTimeZone');
return $ref->getConstants();
}
/**
* This functions acts as a standard way to get the timezones
* regardless of PHP version. It accepts a single parameter,
* zone, which returns the timezones associated with that 'zone'
*
* @since Symphony 2.3
* @link http://au2.php.net/manual/en/class.datetimezone.php
* @link http://au2.php.net/manual/en/datetimezone.listidentifiers.php
* @param string $zone
* The zone for the timezones the field wants. This maps to the
* DateTimeZone constants
* @return array
*/
public static function getTimezones($zone = null)
{
return DateTimeZone::listIdentifiers(constant('DateTimeZone::' . $zone));
}
/**
* Loads all available timezones using `getTimezones()` and builds an
* array where timezones are grouped by their region (Europe/America etc.)
* The options array that is returned is designed to be used with
* `Widget::Select`
*
* @since Symphony 2.3
* @see core.DateTimeObj#getTimezones()
* @see core.Widget#Select()
* @param string $selected
* A preselected timezone, defaults to null
* @return array
* An associative array, for use with `Widget::Select`
*/
public static function getTimezonesSelectOptions($selected = null)
{
$zones = self::getZones();
$groups = array();
foreach ($zones as $zone => $value) {
if ($value >= 1024) {
break;
}
$timezones = self::getTimezones($zone);
$options = array();
foreach ($timezones as $timezone) {
$tz = new DateTime('now', new DateTimeZone($timezone));
$options[] = array($timezone, ($timezone == $selected), sprintf(
"%s %s",
str_replace('_', ' ', substr(strrchr($timezone, '/'), 1)),
$tz->format('P')
));
}
$groups[] = array('label' => ucwords(strtolower($zone)), 'options' => $options);
}
return $groups;
}
/**
* Returns an array of PHP date formats Symphony supports mapped to
* their Moment equivalent.
*
* @since Symphony 2.6
* @return array
*/
public static function getDateFormatMappings()
{
return self::$date_mappings;
}
/**
* Returns an array of the date formats Symphony supports. These
* formats are a combination of valid PHP format tokens.
*
* @link http://au2.php.net/manual/en/function.date.php
* @since Symphony 2.3
* @return array
*/
public static function getDateFormats()
{
return array_keys(self::$date_mappings);
}
/**
* Returns an array of the date formats Symphony supports. These
* formats are a combination of valid Moment format tokens.
*
* @link http://momentjs.com/docs/#/parsing/
* @since Symphony 2.6
* @return array
*/
public static function getMomentDateFormats()
{
return array_values(self::$date_mappings);
}
/**
* Returns the Moment representation of a given PHP format token.
*
* @since Symphony 2.6
* @param string $format
* A valid PHP date token
* @return string
*/
public static function convertDateToMoment($format)
{
return self::$date_mappings[$format];
}
/**
* Returns the PHP representation of a given Moment format token.
*
* @since Symphony 2.6
* @param string $format
* A valid Moment date token
* @return string
*/
public static function convertMomentToDate($format)
{
$formats = array_flip(self::$date_mappings);
return $formats[$format];
}
/**
* Returns an array of the date formats Symphony supports by applying
* the format to the current datetime. The array returned is for use with
* `Widget::Select()`
*
* @since Symphony 2.3
* @see core.Widget#Select()
* @param string $selected
* A preselected date format, defaults to null
* @return array
* An associative array, for use with `Widget::Select`
*/
public static function getDateFormatsSelectOptions($selected = null)
{
$formats = self::getDateFormats();
$options = array();
foreach ($formats as $option) {
$leadingZero = '';
if (strpos($option, 'j') !== false || strpos($option, 'n') !== false) {
$leadingZero = ' (' . __('no leading zeros') . ')';
}
$options[] = array($option, $option == $selected, self::format('now', $option) . $leadingZero);
}
return $options;
}
/**
* Returns an array of the time formats Symphony supports. These
* formats are a combination of valid PHP format tokens.
*
* @link http://au2.php.net/manual/en/function.date.php
* @since Symphony 2.3
* @return array
*/
public static function getTimeFormats()
{
return array_keys(self::$time_mappings);
}
/**
* Returns an array of the time formats Symphony supports. These
* formats are a combination of valid Moment format tokens.
*
* @link http://momentjs.com/docs/#/parsing/
* @since Symphony 2.6
* @return array
*/
public static function getMomentTimeFormats()
{
return array_keys(self::$time_mappings);
}
/**
* Returns the Moment time representation of a given PHP format token.
*
* @since Symphony 2.6
* @param string $format
* A valid PHP time token
* @return string
*/
public static function convertTimeToMoment($format)
{
return self::$time_mappings[$format];
}
/**
* Returns the PHP time representation of a given Moment format token.
*
* @since Symphony 2.6
* @param string $format
* A valid Moment time token
* @return string
*/
public static function convertMomentToTime($format)
{
$formats = array_flip(self::$time_mappings);
return $formats[$format];
}
/**
* Returns an array of the time formats Symphony supports by applying
* the format to the current datetime. The array returned is for use with
* `Widget::Select()`
*
* @since Symphony 2.3
* @see core.Widget#Select()
* @param string $selected
* A preselected time format, defaults to null
* @return array
* An associative array, for use with `Widget::Select`
*/
public static function getTimeFormatsSelectOptions($selected = null)
{
$formats = self::getTimeFormats();
$options = array();
foreach ($formats as $option) {
$options[] = array($option, $option == $selected, self::get($option));
}
return $options;
}
}