/
class.datetimeobj.php
516 lines (462 loc) · 16.1 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
<?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.2.4
* @var array
*/
private static $settings = array();
/**
* 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'];
}
else if (!isset(self::$settings['datetime_separator'])) {
self::$settings['datetime_separator'] = ' ';
}
// Datetime format
if(isset($settings['datetime_format'])) {
self::$settings['datetime_format'] = $settings['datetime_format'];
}
else {
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']);
}
}
/**
* 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 integer $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.
* Please note that for best compatibility with European dates it is recommended
* that your site be in a PHP5.3 environment.
*
* @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
else if (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.
* Please note that for best compatibility with European dates it is recommended
* that your site be in a PHP5.3 environment.
*
* @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`
if(method_exists('DateTime', '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;
}
}
}
// PHP 5.2: Fallback to DateTime parsing.
// Note that this parsing will not respect European dates.
else {
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) {
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 integer $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');
}
/**
* A wrapper for get, this function will return a HTML string representing
* an `<abbr>` element which contained the formatted date of now, and an
* RFC 2822 formatted date (Thu, 21 Dec 2000 16:01:07 +0200) as the title
* attribute. Symphony uses this in it's status messages so that it can
* dynamically update how long ago the action took place using Javascript.
*
* @deprecated This will be removed in the next version of Symphony
* @param string $format
* A valid PHP date format
* @return string
* A HTML string of an `<abbr>` element with a class of 'timeago' and the current
* date (RFC 2822) as the title element. The value is the current time as
* specified by the `$format`.
*/
public static function getTimeAgo($format = __SYM_TIME_FORMAT__){
$time = Widget::Time('', $format);
return $time->generate();
}
/**
* This functions acts as a standard way to get the zones
* available on the system. For PHP5.2, these constants are
* just copied from PHP5.3
*
* @since Symphony 2.3
* @link http://au2.php.net/manual/en/class.datetimezone.php
* @return array
*/
public static function getZones() {
if(PHP_VERSION_ID >= 50300) {
$ref = new ReflectionClass('DateTimeZone');
return $ref->getConstants();
}
else {
return array(
'AFRICA' => 1,
'AMERICA' => 2,
'ANTARCTICA' => 4,
'ARCTIC' => 8,
'ASIA' => 16,
'ATLANTIC' => 32,
'AUSTRALIA' => 64,
'EUROPE' => 128,
'INDIAN' => 256,
'PACIFIC' => 512,
'UTC' => 1024
);
}
}
/**
* 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) {
// PHP5.3 supports the `$what` parameter of the listIdentifiers function
if(PHP_VERSION_ID >= 50300) {
return DateTimeZone::listIdentifiers(constant('DateTimeZone::' . $zone));
}
else {
$timezones = DateTimeZone::listIdentifiers();
foreach($timezones as $index => $timezone) {
if(stripos($timezone, $zone) === false) unset($timezones[$index]);
}
return $timezones;
}
}
/**
* 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 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(
'Y/m/d', // e. g. 2011/01/20
'm/d/Y', // e. g. 01/20/2011
'm/d/y', // e. g. 10/20/11
'Y-m-d', // e. g. 2011-01-20
'm-d-Y', // e. g. 01-20-2011
'm-d-y', // e. g. 01-20-11
'd.m.Y', // e. g. 20.01.2011
'j.n.Y', // e. g. 20.1.2011 - no leading zeros
'd.m.y', // e. g. 20.01.11
'j.n.y', // e. g. 20.1.11 - no leading zeros
'd F Y', // e. g. 20 January 2011
'd M Y', // e. g. 20 Jan 2011
'j. F Y', // e. g. 20. January 2011 - no leading zeros
'j. M. Y', // e. g. 20. Jan. 2011 - no leading zeros
);
}
/**
* 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(
'H:i:s', // e. g. 20:45:32
'H:i', // e. g. 20:45
'g:i:s a', // e. g. 8:45:32 pm
'g:i a', // e. g. 8:45 pm
);
}
/**
* 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;
}
}