-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLocaleConverter.php
More file actions
56 lines (50 loc) · 1.91 KB
/
Copy pathLocaleConverter.php
File metadata and controls
56 lines (50 loc) · 1.91 KB
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
<?php
namespace nkovacs\datetimepicker;
use Yii;
use IntlDateFormatter;
class LocaleConverter
{
protected static $cache = [];
public static function getMonths($format, $locale = null) {
if ($locale === null) {
$locale = Yii::$app->language;
}
if (!isset(static::$cache['months'][$locale][$format])) {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, Yii::$app->timeZone, null, $format);
$localized = [];
for ($month = 0; $month < 12; $month++) {
$localized[] = $formatter->format([
'tm_sec' => 0,
'tm_min' => 0,
'tm_hour' => 12,
'tm_mday' => 15,
'tm_mon' => $month,
'tm_year' => 2016,
]);
}
static::$cache['months'][$locale][$format] = $localized;
}
return static::$cache['months'][$locale][$format];
}
public static function getWeekdays($format, $locale = null) {
if ($locale === null) {
$locale = Yii::$app->language;
}
if (!isset(static::$cache['weekdays'][$locale][$format])) {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, Yii::$app->timeZone, null, $format);
$localized = [];
for ($day = 0; $day < 7; $day++) {
$localized[] = $formatter->format([
'tm_sec' => 0,
'tm_min' => 0,
'tm_hour' => 12,
'tm_mon' => 6,
'tm_year' => 2016,
'tm_mday' => 2 + $day, // 2016-06-02 is a sunday
]);
}
static::$cache['weekdays'][$locale][$format] = $localized;
}
return static::$cache['weekdays'][$locale][$format];
}
}