1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\helpers; |
9
|
|
|
|
10
|
|
|
use IntlDateFormatter; |
11
|
|
|
use Yii; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* BaseFormatConverter provides concrete implementation for [[FormatConverter]]. |
15
|
|
|
* |
16
|
|
|
* Do not use BaseFormatConverter. Use [[FormatConverter]] instead. |
17
|
|
|
* |
18
|
|
|
* @author Carsten Brandt <[email protected]> |
19
|
|
|
* @author Enrica Ruedin <[email protected]> |
20
|
|
|
* @since 2.0 |
21
|
|
|
*/ |
22
|
|
|
class BaseFormatConverter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array the php fallback definition to use for the ICU short patterns `short`, `medium`, `long` and `full`. |
26
|
|
|
* This is used as fallback when the intl extension is not installed. |
27
|
|
|
*/ |
28
|
|
|
public static $phpFallbackDatePatterns = [ |
29
|
|
|
'short' => [ |
30
|
|
|
'date' => 'n/j/y', |
31
|
|
|
'time' => 'H:i', |
32
|
|
|
'datetime' => 'n/j/y H:i', |
33
|
|
|
], |
34
|
|
|
'medium' => [ |
35
|
|
|
'date' => 'M j, Y', |
36
|
|
|
'time' => 'g:i:s A', |
37
|
|
|
'datetime' => 'M j, Y g:i:s A', |
38
|
|
|
], |
39
|
|
|
'long' => [ |
40
|
|
|
'date' => 'F j, Y', |
41
|
|
|
'time' => 'g:i:sA', |
42
|
|
|
'datetime' => 'F j, Y g:i:sA', |
43
|
|
|
], |
44
|
|
|
'full' => [ |
45
|
|
|
'date' => 'l, F j, Y', |
46
|
|
|
'time' => 'g:i:sA T', |
47
|
|
|
'datetime' => 'l, F j, Y g:i:sA T', |
48
|
|
|
], |
49
|
|
|
]; |
50
|
|
|
/** |
51
|
|
|
* @var array the jQuery UI fallback definition to use for the ICU short patterns `short`, `medium`, `long` and `full`. |
52
|
|
|
* This is used as fallback when the intl extension is not installed. |
53
|
|
|
*/ |
54
|
|
|
public static $juiFallbackDatePatterns = [ |
55
|
|
|
'short' => [ |
56
|
|
|
'date' => 'd/m/y', |
57
|
|
|
'time' => '', |
58
|
|
|
'datetime' => 'd/m/y', |
59
|
|
|
], |
60
|
|
|
'medium' => [ |
61
|
|
|
'date' => 'M d, yy', |
62
|
|
|
'time' => '', |
63
|
|
|
'datetime' => 'M d, yy', |
64
|
|
|
], |
65
|
|
|
'long' => [ |
66
|
|
|
'date' => 'MM d, yy', |
67
|
|
|
'time' => '', |
68
|
|
|
'datetime' => 'MM d, yy', |
69
|
|
|
], |
70
|
|
|
'full' => [ |
71
|
|
|
'date' => 'DD, MM d, yy', |
72
|
|
|
'time' => '', |
73
|
|
|
'datetime' => 'DD, MM d, yy', |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
private static $_icuShortFormats = [ |
78
|
|
|
'short' => 3, // IntlDateFormatter::SHORT, |
79
|
|
|
'medium' => 2, // IntlDateFormatter::MEDIUM, |
80
|
|
|
'long' => 1, // IntlDateFormatter::LONG, |
81
|
|
|
'full' => 0, // IntlDateFormatter::FULL, |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Converts a date format pattern from [ICU format][] to [php date() function format][]. |
87
|
|
|
* |
88
|
|
|
* The conversion is limited to date patterns that do not use escaped characters. |
89
|
|
|
* Patterns like `d 'of' MMMM yyyy` which will result in a date like `1 of December 2014` may not be converted correctly |
90
|
|
|
* because of the use of escaped characters. |
91
|
|
|
* |
92
|
|
|
* Pattern constructs that are not supported by the PHP format will be removed. |
93
|
|
|
* |
94
|
|
|
* [php date() function format]: http://php.net/manual/en/function.date.php |
95
|
|
|
* [ICU format]: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax |
96
|
|
|
* |
97
|
|
|
* @param string $pattern date format pattern in ICU format. |
98
|
|
|
* @param string $type 'date', 'time', or 'datetime'. |
99
|
|
|
* @param string $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`. |
100
|
|
|
* If not given, `Yii::$app->language` will be used. |
101
|
|
|
* @return string The converted date format pattern. |
102
|
|
|
*/ |
103
|
180 |
|
public static function convertDateIcuToPhp($pattern, $type = 'date', $locale = null) |
104
|
|
|
{ |
105
|
180 |
|
if (isset(self::$_icuShortFormats[$pattern])) { |
106
|
6 |
|
if (extension_loaded('intl')) { |
107
|
1 |
|
if ($locale === null) { |
108
|
|
|
$locale = Yii::$app->language; |
109
|
|
|
} |
110
|
1 |
|
if ($type === 'date') { |
111
|
1 |
|
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE); |
112
|
|
|
} elseif ($type === 'time') { |
113
|
|
|
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]); |
114
|
|
|
} else { |
115
|
|
|
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]); |
116
|
|
|
} |
117
|
1 |
|
$pattern = $formatter->getPattern(); |
118
|
|
|
} else { |
119
|
5 |
|
return static::$phpFallbackDatePatterns[$pattern][$type]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
// http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax |
123
|
|
|
// escaped text |
124
|
176 |
|
$escaped = []; |
125
|
176 |
|
if (preg_match_all('/(?<!\')\'(.*?[^\'])\'(?!\')/', $pattern, $matches, PREG_SET_ORDER)) { |
126
|
3 |
|
foreach ($matches as $match) { |
127
|
3 |
|
$match[1] = str_replace('\'\'', '\'', $match[1]); |
128
|
3 |
|
$escaped[$match[0]] = '\\' . implode('\\', preg_split('//u', $match[1], -1, PREG_SPLIT_NO_EMPTY)); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
176 |
|
return strtr($pattern, array_merge($escaped, [ |
133
|
176 |
|
"''" => "\\'", // two single quotes produce one |
134
|
|
|
'G' => '', // era designator like (Anno Domini) |
135
|
|
|
'Y' => 'o', // 4digit year of "Week of Year" |
136
|
|
|
'y' => 'Y', // 4digit year e.g. 2014 |
137
|
|
|
'yyyy' => 'Y', // 4digit year e.g. 2014 |
138
|
|
|
'yy' => 'y', // 2digit year number eg. 14 |
139
|
|
|
'u' => '', // extended year e.g. 4601 |
140
|
|
|
'U' => '', // cyclic year name, as in Chinese lunar calendar |
141
|
|
|
'r' => '', // related Gregorian year e.g. 1996 |
142
|
|
|
'Q' => '', // number of quarter |
143
|
|
|
'QQ' => '', // number of quarter '02' |
144
|
|
|
'QQQ' => '', // quarter 'Q2' |
145
|
|
|
'QQQQ' => '', // quarter '2nd quarter' |
146
|
|
|
'QQQQQ' => '', // number of quarter '2' |
147
|
|
|
'q' => '', // number of Stand Alone quarter |
148
|
|
|
'qq' => '', // number of Stand Alone quarter '02' |
149
|
|
|
'qqq' => '', // Stand Alone quarter 'Q2' |
150
|
|
|
'qqqq' => '', // Stand Alone quarter '2nd quarter' |
151
|
|
|
'qqqqq' => '', // number of Stand Alone quarter '2' |
152
|
|
|
'M' => 'n', // Numeric representation of a month, without leading zeros |
153
|
|
|
'MM' => 'm', // Numeric representation of a month, with leading zeros |
154
|
|
|
'MMM' => 'M', // A short textual representation of a month, three letters |
155
|
|
|
'MMMM' => 'F', // A full textual representation of a month, such as January or March |
156
|
|
|
'MMMMM' => '', |
157
|
|
|
'L' => 'n', // Stand alone month in year |
158
|
|
|
'LL' => 'm', // Stand alone month in year |
159
|
|
|
'LLL' => 'M', // Stand alone month in year |
160
|
|
|
'LLLL' => 'F', // Stand alone month in year |
161
|
|
|
'LLLLL' => '', // Stand alone month in year |
162
|
|
|
'w' => 'W', // ISO-8601 week number of year |
163
|
|
|
'ww' => 'W', // ISO-8601 week number of year |
164
|
|
|
'W' => '', // week of the current month |
165
|
|
|
'd' => 'j', // day without leading zeros |
166
|
|
|
'dd' => 'd', // day with leading zeros |
167
|
|
|
'D' => 'z', // day of the year 0 to 365 |
168
|
|
|
'F' => '', // Day of Week in Month. eg. 2nd Wednesday in July |
169
|
|
|
'g' => '', // Modified Julian day. This is different from the conventional Julian day number in two regards. |
170
|
|
|
'E' => 'D', // day of week written in short form eg. Sun |
171
|
|
|
'EE' => 'D', |
172
|
|
|
'EEE' => 'D', |
173
|
|
|
'EEEE' => 'l', // day of week fully written eg. Sunday |
174
|
|
|
'EEEEE' => '', |
175
|
|
|
'EEEEEE' => '', |
176
|
|
|
'e' => 'N', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun |
177
|
|
|
'ee' => 'N', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year |
178
|
|
|
'eee' => 'D', |
179
|
|
|
'eeee' => 'l', |
180
|
|
|
'eeeee' => '', |
181
|
|
|
'eeeeee' => '', |
182
|
|
|
'c' => 'N', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun |
183
|
|
|
'cc' => 'N', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year |
184
|
|
|
'ccc' => 'D', |
185
|
|
|
'cccc' => 'l', |
186
|
|
|
'ccccc' => '', |
187
|
|
|
'cccccc' => '', |
188
|
|
|
'a' => 'a', // am/pm marker |
189
|
|
|
'h' => 'g', // 12-hour format of an hour without leading zeros 1 to 12h |
190
|
|
|
'hh' => 'h', // 12-hour format of an hour with leading zeros, 01 to 12 h |
191
|
|
|
'H' => 'G', // 24-hour format of an hour without leading zeros 0 to 23h |
192
|
|
|
'HH' => 'H', // 24-hour format of an hour with leading zeros, 00 to 23 h |
193
|
|
|
'k' => '', // hour in day (1~24) |
194
|
|
|
'kk' => '', // hour in day (1~24) |
195
|
|
|
'K' => '', // hour in am/pm (0~11) |
196
|
|
|
'KK' => '', // hour in am/pm (0~11) |
197
|
|
|
'm' => 'i', // Minutes without leading zeros, not supported by php but we fallback |
198
|
|
|
'mm' => 'i', // Minutes with leading zeros |
199
|
|
|
's' => 's', // Seconds, without leading zeros, not supported by php but we fallback |
200
|
|
|
'ss' => 's', // Seconds, with leading zeros |
201
|
|
|
'S' => '', // fractional second |
202
|
|
|
'SS' => '', // fractional second |
203
|
|
|
'SSS' => '', // fractional second |
204
|
|
|
'SSSS' => '', // fractional second |
205
|
|
|
'A' => '', // milliseconds in day |
206
|
|
|
'z' => 'T', // Timezone abbreviation |
207
|
|
|
'zz' => 'T', // Timezone abbreviation |
208
|
|
|
'zzz' => 'T', // Timezone abbreviation |
209
|
|
|
'zzzz' => 'T', // Timezone full name, not supported by php but we fallback |
210
|
|
|
'Z' => 'O', // Difference to Greenwich time (GMT) in hours |
211
|
|
|
'ZZ' => 'O', // Difference to Greenwich time (GMT) in hours |
212
|
|
|
'ZZZ' => 'O', // Difference to Greenwich time (GMT) in hours |
213
|
|
|
'ZZZZ' => '\G\M\TP', // Time Zone: long localized GMT (=OOOO) e.g. GMT-08:00 |
214
|
|
|
'ZZZZZ' => '', // TIme Zone: ISO8601 extended hms? (=XXXXX) |
215
|
|
|
'O' => '', // Time Zone: short localized GMT e.g. GMT-8 |
216
|
|
|
'OOOO' => '\G\M\TP', // Time Zone: long localized GMT (=ZZZZ) e.g. GMT-08:00 |
217
|
|
|
'v' => '\G\M\TP', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here |
218
|
|
|
'vvvv' => '\G\M\TP', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here |
219
|
|
|
'V' => '', // Time Zone: short time zone ID |
220
|
|
|
'VV' => 'e', // Time Zone: long time zone ID |
221
|
|
|
'VVV' => '', // Time Zone: time zone exemplar city |
222
|
|
|
'VVVV' => '\G\M\TP', // Time Zone: generic location (falls back to OOOO) using the ICU defined fallback here |
223
|
|
|
'X' => '', // Time Zone: ISO8601 basic hm?, with Z for 0, e.g. -08, +0530, Z |
224
|
|
|
'XX' => 'O, \Z', // Time Zone: ISO8601 basic hm, with Z, e.g. -0800, Z |
225
|
|
|
'XXX' => 'P, \Z', // Time Zone: ISO8601 extended hm, with Z, e.g. -08:00, Z |
226
|
|
|
'XXXX' => '', // Time Zone: ISO8601 basic hms?, with Z, e.g. -0800, -075258, Z |
227
|
|
|
'XXXXX' => '', // Time Zone: ISO8601 extended hms?, with Z, e.g. -08:00, -07:52:58, Z |
228
|
|
|
'x' => '', // Time Zone: ISO8601 basic hm?, without Z for 0, e.g. -08, +0530 |
229
|
|
|
'xx' => 'O', // Time Zone: ISO8601 basic hm, without Z, e.g. -0800 |
230
|
|
|
'xxx' => 'P', // Time Zone: ISO8601 extended hm, without Z, e.g. -08:00 |
231
|
|
|
'xxxx' => '', // Time Zone: ISO8601 basic hms?, without Z, e.g. -0800, -075258 |
232
|
|
|
'xxxxx' => '', // Time Zone: ISO8601 extended hms?, without Z, e.g. -08:00, -07:52:58 |
233
|
|
|
])); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Converts a date format pattern from [php date() function format][] to [ICU format][]. |
238
|
|
|
* |
239
|
|
|
* Pattern constructs that are not supported by the ICU format will be removed. |
240
|
|
|
* |
241
|
|
|
* [php date() function format]: http://php.net/manual/en/function.date.php |
242
|
|
|
* [ICU format]: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax |
243
|
|
|
* |
244
|
|
|
* Since 2.0.13 it handles escaped characters correctly. |
245
|
|
|
* |
246
|
|
|
* @param string $pattern date format pattern in php date()-function format. |
247
|
|
|
* @return string The converted date format pattern. |
248
|
|
|
*/ |
249
|
7 |
|
public static function convertDatePhpToIcu($pattern) |
250
|
|
|
{ |
251
|
|
|
// http://php.net/manual/en/function.date.php |
252
|
7 |
|
$result = strtr($pattern, [ |
253
|
7 |
|
"'" => "''''", // single `'` should be encoded as `''`, which internally should be encoded as `''''` |
254
|
|
|
// Day |
255
|
|
|
'\d' => "'d'", |
256
|
|
|
'd' => 'dd', // Day of the month, 2 digits with leading zeros 01 to 31 |
257
|
|
|
'\D' => "'D'", |
258
|
|
|
'D' => 'eee', // A textual representation of a day, three letters Mon through Sun |
259
|
|
|
'\j' => "'j'", |
260
|
|
|
'j' => 'd', // Day of the month without leading zeros 1 to 31 |
261
|
|
|
'\l' => "'l'", |
262
|
|
|
'l' => 'eeee', // A full textual representation of the day of the week Sunday through Saturday |
263
|
|
|
'\N' => "'N'", |
264
|
|
|
'N' => 'e', // ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday) |
265
|
|
|
'\S' => "'S'", |
266
|
|
|
'S' => '', // English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j |
267
|
|
|
'\w' => "'w'", |
268
|
|
|
'w' => '', // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) |
269
|
|
|
'\z' => "'z'", |
270
|
|
|
'z' => 'D', // The day of the year (starting from 0) 0 through 365 |
271
|
|
|
// Week |
272
|
|
|
'\W' => "'W'", |
273
|
|
|
'W' => 'w', // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year) |
274
|
|
|
// Month |
275
|
|
|
'\F' => "'F'", |
276
|
|
|
'F' => 'MMMM', // A full textual representation of a month, January through December |
277
|
|
|
'\m' => "'m'", |
278
|
|
|
'm' => 'MM', // Numeric representation of a month, with leading zeros 01 through 12 |
279
|
|
|
'\M' => "'M'", |
280
|
|
|
'M' => 'MMM', // A short textual representation of a month, three letters Jan through Dec |
281
|
|
|
'\n' => "'n'", |
282
|
|
|
'n' => 'M', // Numeric representation of a month, without leading zeros 1 through 12, not supported by ICU but we fallback to "with leading zero" |
283
|
|
|
'\t' => "'t'", |
284
|
|
|
't' => '', // Number of days in the given month 28 through 31 |
285
|
|
|
// Year |
286
|
|
|
'\L' => "'L'", |
287
|
|
|
'L' => '', // Whether it's a leap year, 1 if it is a leap year, 0 otherwise. |
288
|
|
|
'\o' => "'o'", |
289
|
|
|
'o' => 'Y', // ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. |
290
|
|
|
'\Y' => "'Y'", |
291
|
|
|
'Y' => 'yyyy', // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 |
292
|
|
|
'\y' => "'y'", |
293
|
|
|
'y' => 'yy', // A two digit representation of a year Examples: 99 or 03 |
294
|
|
|
// Time |
295
|
|
|
'\a' => "'a'", |
296
|
|
|
'a' => 'a', // Lowercase Ante meridiem and Post meridiem, am or pm |
297
|
|
|
'\A' => "'A'", |
298
|
|
|
'A' => 'a', // Uppercase Ante meridiem and Post meridiem, AM or PM, not supported by ICU but we fallback to lowercase |
299
|
|
|
'\B' => "'B'", |
300
|
|
|
'B' => '', // Swatch Internet time 000 through 999 |
301
|
|
|
'\g' => "'g'", |
302
|
|
|
'g' => 'h', // 12-hour format of an hour without leading zeros 1 through 12 |
303
|
|
|
'\G' => "'G'", |
304
|
|
|
'G' => 'H', // 24-hour format of an hour without leading zeros 0 to 23h |
305
|
|
|
'\h' => "'h'", |
306
|
|
|
'h' => 'hh', // 12-hour format of an hour with leading zeros, 01 to 12 h |
307
|
|
|
'\H' => "'H'", |
308
|
|
|
'H' => 'HH', // 24-hour format of an hour with leading zeros, 00 to 23 h |
309
|
|
|
'\i' => "'i'", |
310
|
|
|
'i' => 'mm', // Minutes with leading zeros 00 to 59 |
311
|
|
|
'\s' => "'s'", |
312
|
|
|
's' => 'ss', // Seconds, with leading zeros 00 through 59 |
313
|
|
|
'\u' => "'u'", |
314
|
|
|
'u' => '', // Microseconds. Example: 654321 |
315
|
|
|
// Timezone |
316
|
|
|
'\e' => "'e'", |
317
|
|
|
'e' => 'VV', // Timezone identifier. Examples: UTC, GMT, Atlantic/Azores |
318
|
|
|
'\I' => "'I'", |
319
|
|
|
'I' => '', // Whether or not the date is in daylight saving time, 1 if Daylight Saving Time, 0 otherwise. |
320
|
|
|
'\O' => "'O'", |
321
|
|
|
'O' => 'xx', // Difference to Greenwich time (GMT) in hours, Example: +0200 |
322
|
|
|
'\P' => "'P'", |
323
|
|
|
'P' => 'xxx', // Difference to Greenwich time (GMT) with colon between hours and minutes, Example: +02:00 |
324
|
|
|
'\T' => "'T'", |
325
|
|
|
'T' => 'zzz', // Timezone abbreviation, Examples: EST, MDT ... |
326
|
|
|
'\Z' => "'Z'", |
327
|
|
|
'Z' => '', // Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through 50400 |
328
|
|
|
// Full Date/Time |
329
|
|
|
'\c' => "'c'", |
330
|
|
|
'c' => "yyyy-MM-dd'T'HH:mm:ssxxx", // ISO 8601 date, e.g. 2004-02-12T15:19:21+00:00 |
331
|
|
|
'\r' => "'r'", |
332
|
|
|
'r' => 'eee, dd MMM yyyy HH:mm:ss xx', // RFC 2822 formatted date, Example: Thu, 21 Dec 2000 16:01:07 +0200 |
333
|
|
|
'\U' => "'U'", |
334
|
|
|
'U' => '', // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) |
335
|
|
|
'\\\\' => '\\', |
336
|
|
|
]); |
337
|
|
|
|
338
|
|
|
// remove `''` - the're result of consecutive escaped chars (`\A\B` will be `'A''B'`, but should be `'AB'`) |
339
|
|
|
// real `'` are encoded as `''''` |
340
|
7 |
|
return strtr($result, [ |
341
|
7 |
|
"''''" => "''", |
342
|
|
|
"''" => '', |
343
|
|
|
]); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Converts a date format pattern from [ICU format][] to [jQuery UI date format][]. |
348
|
|
|
* |
349
|
|
|
* Pattern constructs that are not supported by the jQuery UI format will be removed. |
350
|
|
|
* |
351
|
|
|
* [jQuery UI date format]: http://api.jqueryui.com/datepicker/#utility-formatDate |
352
|
|
|
* [ICU format]: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax |
353
|
|
|
* |
354
|
|
|
* @param string $pattern date format pattern in ICU format. |
355
|
|
|
* @param string $type 'date', 'time', or 'datetime'. |
356
|
|
|
* @param string $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`. |
357
|
|
|
* If not given, `Yii::$app->language` will be used. |
358
|
|
|
* @return string The converted date format pattern. |
359
|
|
|
*/ |
360
|
2 |
|
public static function convertDateIcuToJui($pattern, $type = 'date', $locale = null) |
361
|
|
|
{ |
362
|
2 |
|
if (isset(self::$_icuShortFormats[$pattern])) { |
363
|
|
|
if (extension_loaded('intl')) { |
364
|
|
|
if ($locale === null) { |
365
|
|
|
$locale = Yii::$app->language; |
366
|
|
|
} |
367
|
|
|
if ($type === 'date') { |
368
|
|
|
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE); |
369
|
|
|
} elseif ($type === 'time') { |
370
|
|
|
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]); |
371
|
|
|
} else { |
372
|
|
|
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]); |
373
|
|
|
} |
374
|
|
|
$pattern = $formatter->getPattern(); |
375
|
|
|
} else { |
376
|
|
|
return static::$juiFallbackDatePatterns[$pattern][$type]; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
// http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax |
380
|
|
|
// escaped text |
381
|
2 |
|
$escaped = []; |
382
|
2 |
|
if (preg_match_all('/(?<!\')\'.*?[^\']\'(?!\')/', $pattern, $matches)) { |
383
|
2 |
|
foreach ($matches[0] as $match) { |
384
|
2 |
|
$escaped[$match] = $match; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
2 |
|
return strtr($pattern, array_merge($escaped, [ |
389
|
2 |
|
'G' => '', // era designator like (Anno Domini) |
390
|
|
|
'Y' => '', // 4digit year of "Week of Year" |
391
|
|
|
'y' => 'yy', // 4digit year e.g. 2014 |
392
|
|
|
'yyyy' => 'yy', // 4digit year e.g. 2014 |
393
|
|
|
'yy' => 'y', // 2digit year number eg. 14 |
394
|
|
|
'u' => '', // extended year e.g. 4601 |
395
|
|
|
'U' => '', // cyclic year name, as in Chinese lunar calendar |
396
|
|
|
'r' => '', // related Gregorian year e.g. 1996 |
397
|
|
|
'Q' => '', // number of quarter |
398
|
|
|
'QQ' => '', // number of quarter '02' |
399
|
|
|
'QQQ' => '', // quarter 'Q2' |
400
|
|
|
'QQQQ' => '', // quarter '2nd quarter' |
401
|
|
|
'QQQQQ' => '', // number of quarter '2' |
402
|
|
|
'q' => '', // number of Stand Alone quarter |
403
|
|
|
'qq' => '', // number of Stand Alone quarter '02' |
404
|
|
|
'qqq' => '', // Stand Alone quarter 'Q2' |
405
|
|
|
'qqqq' => '', // Stand Alone quarter '2nd quarter' |
406
|
|
|
'qqqqq' => '', // number of Stand Alone quarter '2' |
407
|
|
|
'M' => 'm', // Numeric representation of a month, without leading zeros |
408
|
|
|
'MM' => 'mm', // Numeric representation of a month, with leading zeros |
409
|
|
|
'MMM' => 'M', // A short textual representation of a month, three letters |
410
|
|
|
'MMMM' => 'MM', // A full textual representation of a month, such as January or March |
411
|
|
|
'MMMMM' => '', |
412
|
|
|
'L' => 'm', // Stand alone month in year |
413
|
|
|
'LL' => 'mm', // Stand alone month in year |
414
|
|
|
'LLL' => 'M', // Stand alone month in year |
415
|
|
|
'LLLL' => 'MM', // Stand alone month in year |
416
|
|
|
'LLLLL' => '', // Stand alone month in year |
417
|
|
|
'w' => '', // ISO-8601 week number of year |
418
|
|
|
'ww' => '', // ISO-8601 week number of year |
419
|
|
|
'W' => '', // week of the current month |
420
|
|
|
'd' => 'd', // day without leading zeros |
421
|
|
|
'dd' => 'dd', // day with leading zeros |
422
|
|
|
'D' => 'o', // day of the year 0 to 365 |
423
|
|
|
'F' => '', // Day of Week in Month. eg. 2nd Wednesday in July |
424
|
|
|
'g' => '', // Modified Julian day. This is different from the conventional Julian day number in two regards. |
425
|
|
|
'E' => 'D', // day of week written in short form eg. Sun |
426
|
|
|
'EE' => 'D', |
427
|
|
|
'EEE' => 'D', |
428
|
|
|
'EEEE' => 'DD', // day of week fully written eg. Sunday |
429
|
|
|
'EEEEE' => '', |
430
|
|
|
'EEEEEE' => '', |
431
|
|
|
'e' => '', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun |
432
|
|
|
'ee' => '', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year |
433
|
|
|
'eee' => 'D', |
434
|
|
|
'eeee' => '', |
435
|
|
|
'eeeee' => '', |
436
|
|
|
'eeeeee' => '', |
437
|
|
|
'c' => '', // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun |
438
|
|
|
'cc' => '', // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year |
439
|
|
|
'ccc' => 'D', |
440
|
|
|
'cccc' => 'DD', |
441
|
|
|
'ccccc' => '', |
442
|
|
|
'cccccc' => '', |
443
|
|
|
'a' => '', // am/pm marker |
444
|
|
|
'h' => '', // 12-hour format of an hour without leading zeros 1 to 12h |
445
|
|
|
'hh' => '', // 12-hour format of an hour with leading zeros, 01 to 12 h |
446
|
|
|
'H' => '', // 24-hour format of an hour without leading zeros 0 to 23h |
447
|
|
|
'HH' => '', // 24-hour format of an hour with leading zeros, 00 to 23 h |
448
|
|
|
'k' => '', // hour in day (1~24) |
449
|
|
|
'kk' => '', // hour in day (1~24) |
450
|
|
|
'K' => '', // hour in am/pm (0~11) |
451
|
|
|
'KK' => '', // hour in am/pm (0~11) |
452
|
|
|
'm' => '', // Minutes without leading zeros, not supported by php but we fallback |
453
|
|
|
'mm' => '', // Minutes with leading zeros |
454
|
|
|
's' => '', // Seconds, without leading zeros, not supported by php but we fallback |
455
|
|
|
'ss' => '', // Seconds, with leading zeros |
456
|
|
|
'S' => '', // fractional second |
457
|
|
|
'SS' => '', // fractional second |
458
|
|
|
'SSS' => '', // fractional second |
459
|
|
|
'SSSS' => '', // fractional second |
460
|
|
|
'A' => '', // milliseconds in day |
461
|
|
|
'z' => '', // Timezone abbreviation |
462
|
|
|
'zz' => '', // Timezone abbreviation |
463
|
|
|
'zzz' => '', // Timezone abbreviation |
464
|
|
|
'zzzz' => '', // Timezone full name, not supported by php but we fallback |
465
|
|
|
'Z' => '', // Difference to Greenwich time (GMT) in hours |
466
|
|
|
'ZZ' => '', // Difference to Greenwich time (GMT) in hours |
467
|
|
|
'ZZZ' => '', // Difference to Greenwich time (GMT) in hours |
468
|
|
|
'ZZZZ' => '', // Time Zone: long localized GMT (=OOOO) e.g. GMT-08:00 |
469
|
|
|
'ZZZZZ' => '', // Time Zone: ISO8601 extended hms? (=XXXXX) |
470
|
|
|
'O' => '', // Time Zone: short localized GMT e.g. GMT-8 |
471
|
|
|
'OOOO' => '', // Time Zone: long localized GMT (=ZZZZ) e.g. GMT-08:00 |
472
|
|
|
'v' => '', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here |
473
|
|
|
'vvvv' => '', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here |
474
|
|
|
'V' => '', // Time Zone: short time zone ID |
475
|
|
|
'VV' => '', // Time Zone: long time zone ID |
476
|
|
|
'VVV' => '', // Time Zone: time zone exemplar city |
477
|
|
|
'VVVV' => '', // Time Zone: generic location (falls back to OOOO) using the ICU defined fallback here |
478
|
|
|
'X' => '', // Time Zone: ISO8601 basic hm?, with Z for 0, e.g. -08, +0530, Z |
479
|
|
|
'XX' => '', // Time Zone: ISO8601 basic hm, with Z, e.g. -0800, Z |
480
|
|
|
'XXX' => '', // Time Zone: ISO8601 extended hm, with Z, e.g. -08:00, Z |
481
|
|
|
'XXXX' => '', // Time Zone: ISO8601 basic hms?, with Z, e.g. -0800, -075258, Z |
482
|
|
|
'XXXXX' => '', // Time Zone: ISO8601 extended hms?, with Z, e.g. -08:00, -07:52:58, Z |
483
|
|
|
'x' => '', // Time Zone: ISO8601 basic hm?, without Z for 0, e.g. -08, +0530 |
484
|
|
|
'xx' => '', // Time Zone: ISO8601 basic hm, without Z, e.g. -0800 |
485
|
|
|
'xxx' => '', // Time Zone: ISO8601 extended hm, without Z, e.g. -08:00 |
486
|
|
|
'xxxx' => '', // Time Zone: ISO8601 basic hms?, without Z, e.g. -0800, -075258 |
487
|
|
|
'xxxxx' => '', // Time Zone: ISO8601 extended hms?, without Z, e.g. -08:00, -07:52:58 |
488
|
|
|
])); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Converts a date format pattern from [php date() function format][] to [jQuery UI date format][]. |
493
|
|
|
* |
494
|
|
|
* The conversion is limited to date patterns that do not use escaped characters. |
495
|
|
|
* Patterns like `jS \o\f F Y` which will result in a date like `1st of December 2014` may not be converted correctly |
496
|
|
|
* because of the use of escaped characters. |
497
|
|
|
* |
498
|
|
|
* Pattern constructs that are not supported by the jQuery UI format will be removed. |
499
|
|
|
* |
500
|
|
|
* [php date() function format]: http://php.net/manual/en/function.date.php |
501
|
|
|
* [jQuery UI date format]: http://api.jqueryui.com/datepicker/#utility-formatDate |
502
|
|
|
* |
503
|
|
|
* @param string $pattern date format pattern in php date()-function format. |
504
|
|
|
* @return string The converted date format pattern. |
505
|
|
|
*/ |
506
|
|
|
public static function convertDatePhpToJui($pattern) |
507
|
|
|
{ |
508
|
|
|
// http://php.net/manual/en/function.date.php |
509
|
|
|
return strtr($pattern, [ |
510
|
|
|
// Day |
511
|
|
|
'd' => 'dd', // Day of the month, 2 digits with leading zeros 01 to 31 |
512
|
|
|
'D' => 'D', // A textual representation of a day, three letters Mon through Sun |
513
|
|
|
'j' => 'd', // Day of the month without leading zeros 1 to 31 |
514
|
|
|
'l' => 'DD', // A full textual representation of the day of the week Sunday through Saturday |
515
|
|
|
'N' => '', // ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday) |
516
|
|
|
'S' => '', // English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j |
517
|
|
|
'w' => '', // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) |
518
|
|
|
'z' => 'o', // The day of the year (starting from 0) 0 through 365 |
519
|
|
|
// Week |
520
|
|
|
'W' => '', // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year) |
521
|
|
|
// Month |
522
|
|
|
'F' => 'MM', // A full textual representation of a month, January through December |
523
|
|
|
'm' => 'mm', // Numeric representation of a month, with leading zeros 01 through 12 |
524
|
|
|
'M' => 'M', // A short textual representation of a month, three letters Jan through Dec |
525
|
|
|
'n' => 'm', // Numeric representation of a month, without leading zeros 1 through 12 |
526
|
|
|
't' => '', // Number of days in the given month 28 through 31 |
527
|
|
|
// Year |
528
|
|
|
'L' => '', // Whether it's a leap year, 1 if it is a leap year, 0 otherwise. |
529
|
|
|
'o' => '', // ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. |
530
|
|
|
'Y' => 'yy', // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 |
531
|
|
|
'y' => 'y', // A two digit representation of a year Examples: 99 or 03 |
532
|
|
|
// Time |
533
|
|
|
'a' => '', // Lowercase Ante meridiem and Post meridiem, am or pm |
534
|
|
|
'A' => '', // Uppercase Ante meridiem and Post meridiem, AM or PM, not supported by ICU but we fallback to lowercase |
535
|
|
|
'B' => '', // Swatch Internet time 000 through 999 |
536
|
|
|
'g' => '', // 12-hour format of an hour without leading zeros 1 through 12 |
537
|
|
|
'G' => '', // 24-hour format of an hour without leading zeros 0 to 23h |
538
|
|
|
'h' => '', // 12-hour format of an hour with leading zeros, 01 to 12 h |
539
|
|
|
'H' => '', // 24-hour format of an hour with leading zeros, 00 to 23 h |
540
|
|
|
'i' => '', // Minutes with leading zeros 00 to 59 |
541
|
|
|
's' => '', // Seconds, with leading zeros 00 through 59 |
542
|
|
|
'u' => '', // Microseconds. Example: 654321 |
543
|
|
|
// Timezone |
544
|
|
|
'e' => '', // Timezone identifier. Examples: UTC, GMT, Atlantic/Azores |
545
|
|
|
'I' => '', // Whether or not the date is in daylight saving time, 1 if Daylight Saving Time, 0 otherwise. |
546
|
|
|
'O' => '', // Difference to Greenwich time (GMT) in hours, Example: +0200 |
547
|
|
|
'P' => '', // Difference to Greenwich time (GMT) with colon between hours and minutes, Example: +02:00 |
548
|
|
|
'T' => '', // Timezone abbreviation, Examples: EST, MDT ... |
549
|
|
|
'Z' => '', // Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through 50400 |
550
|
|
|
// Full Date/Time |
551
|
|
|
'c' => 'yyyy-MM-dd', // ISO 8601 date, e.g. 2004-02-12T15:19:21+00:00, skipping the time here because it is not supported |
552
|
|
|
'r' => 'D, d M yy', // RFC 2822 formatted date, Example: Thu, 21 Dec 2000 16:01:07 +0200, skipping the time here because it is not supported |
553
|
|
|
'U' => '@', // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) |
554
|
|
|
]); |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
|