1 <?php
2
3 namespace Docolight\Support;
4
5 use Carbon\Carbon;
6
7 8 9 10 11
12 class Carbonate
13 {
14 protected static $month = array(
15 'Jan' => 1,
16 'Feb' => 2,
17 'Mar' => 3,
18 'Apr' => 4,
19 'May' => 5,
20 'Jun' => 6,
21 'Jul' => 7,
22 'Aug' => 8,
23 'Sep' => 9,
24 'Oct' => 10,
25 'Nov' => 11,
26 'Dev' => 12,
27 );
28
29 protected static $day = array(
30 'Mon' => 1,
31 'Tue' => 2,
32 'Wed' => 3,
33 'Thu' => 4,
34 'Fri' => 5,
35 'Sat' => 6,
36 'Sun' => 7,
37 );
38
39 protected static $localeMonth = array(
40 'id' => array(
41 1 => 'Januari',
42 2 => 'Februari',
43 3 => 'Maret',
44 4 => 'April',
45 5 => 'Mei',
46 6 => 'Juni',
47 7 => 'Juli',
48 8 => 'Agustus',
49 9 => 'September',
50 10 => 'Oktober',
51 11 => 'November',
52 12 => 'Desember',
53 ),
54 );
55
56 protected static $localeDays = array(
57 'id' => array(
58 1 => 'Senin',
59 2 => 'Selasa',
60 3 => 'Rabu',
61 4 => 'Kamis',
62 5 => 'Jum\'at',
63 6 => 'Sabtu',
64 7 => 'Minggu',
65 ),
66 );
67
68 69 70 71 72 73 74 75 76
77 public static function formatId($date, $format = 'Y-m-d H:i:s', $returnFormat = 'D, d M Y')
78 {
79 $date = Carbon::createFromFormat($format, $date);
80
81 return ($date) ? static::formatLocale($date, 'id', $returnFormat)
82 : null;
83 }
84
85 86 87 88 89 90 91 92 93
94 public static function formatLocale(Carbon $date, $locale, $returnFormat)
95 {
96 $date->setLocale('en');
97
98 $replacedMonth = str_replace($date->format('M'), static::localeMonth($date, $locale), $date->format($returnFormat));
99 $replacedDay = str_replace($date->format('D'), static::localeDay($date, $locale), $replacedMonth);
100
101 return $replacedDay;
102 }
103
104 105 106 107 108 109 110
111 public static function monthIndex(Carbon $date)
112 {
113 $date->setLocale('en');
114
115 return def(static::$month, $date->format('M'));
116 }
117
118 public static function dayIndex(Carbon $date)
119 {
120 $date->setLocale('en');
121
122 return def(static::$day, $date->format('D'));
123 }
124
125 126 127 128 129 130 131 132
133 public static function localeMonth(Carbon $date, $locale)
134 {
135 return def(static::localeMonths($locale), static::monthIndex($date));
136 }
137
138 139 140 141 142 143 144 145
146 public static function localeDay(Carbon $date, $locale)
147 {
148 return def(static::localeDays($locale), static::dayIndex($date));
149 }
150
151 152 153 154 155
156 public static function localeDays($locale)
157 {
158 return def(static::$localeDays, $locale, array());
159 }
160
161 162 163 164 165
166 public static function localeMonths($locale)
167 {
168 return def(static::$localeMonth, $locale, array());
169 }
170
171 172 173 174 175 176 177 178 179
180 public static function diff($start, $end, $format = 'Y-m-d')
181 {
182 $start = Carbon::createFromFormat($format, $start);
183 $end = Carbon::createFromFormat($format, $end);
184
185 return ($start and $end) ? $start->diffInDays($end)
186 : null;
187 }
188 }
189