1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pratiksh\Nepalidate\Services; |
4
|
|
|
|
5
|
|
|
abstract class DateConverter |
6
|
|
|
{ |
7
|
|
|
public $year; |
8
|
|
|
public $month; |
9
|
|
|
public $day; |
10
|
|
|
|
11
|
|
|
protected $calendarData = [ |
12
|
|
|
0 => [2000, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
13
|
|
|
1 => [2001, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
14
|
|
|
2 => [2002, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
15
|
|
|
3 => [2003, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
16
|
|
|
4 => [2004, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
17
|
|
|
5 => [2005, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
18
|
|
|
6 => [2006, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
19
|
|
|
7 => [2007, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
20
|
|
|
8 => [2008, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], |
21
|
|
|
9 => [2009, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
22
|
|
|
10 => [2010, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
23
|
|
|
11 => [2011, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
24
|
|
|
12 => [2012, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], |
25
|
|
|
13 => [2013, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
26
|
|
|
14 => [2014, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
27
|
|
|
15 => [2015, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
28
|
|
|
16 => [2016, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], |
29
|
|
|
17 => [2017, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
30
|
|
|
18 => [2018, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
31
|
|
|
19 => [2019, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
32
|
|
|
20 => [2020, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
33
|
|
|
21 => [2021, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
34
|
|
|
22 => [2022, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], |
35
|
|
|
23 => [2023, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
36
|
|
|
24 => [2024, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
37
|
|
|
25 => [2025, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
38
|
|
|
26 => [2026, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
39
|
|
|
27 => [2027, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
40
|
|
|
28 => [2028, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
41
|
|
|
29 => [2029, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], |
42
|
|
|
30 => [2030, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
43
|
|
|
31 => [2031, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
44
|
|
|
32 => [2032, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
45
|
|
|
33 => [2033, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
46
|
|
|
34 => [2034, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
47
|
|
|
35 => [2035, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], |
48
|
|
|
36 => [2036, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
49
|
|
|
37 => [2037, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
50
|
|
|
38 => [2038, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
51
|
|
|
39 => [2039, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], |
52
|
|
|
40 => [2040, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
53
|
|
|
41 => [2041, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
54
|
|
|
42 => [2042, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
55
|
|
|
43 => [2043, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], |
56
|
|
|
44 => [2044, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
57
|
|
|
45 => [2045, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
58
|
|
|
46 => [2046, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
59
|
|
|
47 => [2047, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
60
|
|
|
48 => [2048, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
61
|
|
|
49 => [2049, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], |
62
|
|
|
50 => [2050, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
63
|
|
|
51 => [2051, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
64
|
|
|
52 => [2052, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
65
|
|
|
53 => [2053, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], |
66
|
|
|
54 => [2054, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
67
|
|
|
55 => [2055, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
68
|
|
|
56 => [2056, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], |
69
|
|
|
57 => [2057, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
70
|
|
|
58 => [2058, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
71
|
|
|
59 => [2059, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
72
|
|
|
60 => [2060, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
73
|
|
|
61 => [2061, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
74
|
|
|
62 => [2062, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], |
75
|
|
|
63 => [2063, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
76
|
|
|
64 => [2064, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
77
|
|
|
65 => [2065, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
78
|
|
|
66 => [2066, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], |
79
|
|
|
67 => [2067, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
80
|
|
|
68 => [2068, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
81
|
|
|
69 => [2069, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
82
|
|
|
70 => [2070, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], |
83
|
|
|
71 => [2071, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
84
|
|
|
72 => [2072, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], |
85
|
|
|
73 => [2073, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], |
86
|
|
|
74 => [2074, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
87
|
|
|
75 => [2075, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
88
|
|
|
76 => [2076, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], |
89
|
|
|
77 => [2077, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
90
|
|
|
78 => [2078, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
91
|
|
|
79 => [2079, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], |
92
|
|
|
80 => [2080, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], |
93
|
|
|
81 => [2081, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 29, 31], |
94
|
|
|
82 => [2082, 31, 32, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], |
95
|
|
|
83 => [2083, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], |
96
|
|
|
84 => [2084, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], |
97
|
|
|
85 => [2085, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], |
98
|
|
|
86 => [2086, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], |
99
|
|
|
87 => [2087, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], |
100
|
|
|
88 => [2088, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], |
101
|
|
|
89 => [2089, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], |
102
|
|
|
90 => [2090, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
private $bsMonthInEnglish = [ |
106
|
|
|
1 => 'Baisakh', |
107
|
|
|
2 => 'Jestha', |
108
|
|
|
3 => 'Ashar', |
109
|
|
|
4 => 'Shrawan', |
110
|
|
|
5 => 'Bhadra', |
111
|
|
|
6 => 'Ashoj', |
112
|
|
|
7 => 'Kartik', |
113
|
|
|
8 => 'Mangsir', |
114
|
|
|
9 => 'Poush', |
115
|
|
|
10 => 'Magh', |
116
|
|
|
11 => 'Falgun', |
117
|
|
|
12 => 'Chaitra', |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
private $dayOfWeekInEnglish = [ |
121
|
|
|
1 => 'Sunday', |
122
|
|
|
2 => 'Monday', |
123
|
|
|
3 => 'Tuesday', |
124
|
|
|
4 => 'Wednesday', |
125
|
|
|
5 => 'Thursday', |
126
|
|
|
6 => 'Friday', |
127
|
|
|
7 => 'Saturday', |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
private $bsMonthInNepali = [ |
131
|
|
|
1 => 'वैशाख', |
132
|
|
|
2 => 'जेठ', |
133
|
|
|
3 => 'असार', |
134
|
|
|
4 => 'साउन', |
135
|
|
|
5 => 'भदौ', |
136
|
|
|
6 => 'असोज', |
137
|
|
|
7 => 'कार्तिक', |
138
|
|
|
8 => 'मंसिर', |
139
|
|
|
9 => 'पुष', |
140
|
|
|
10 => 'माघ', |
141
|
|
|
11 => 'फागुन', |
142
|
|
|
12 => 'चैत', |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
private $adMonthInNepali = [ |
146
|
|
|
1 => 'जनवरी', |
147
|
|
|
2 => 'फेब्रुवरी', |
148
|
|
|
3 => 'मार्च', |
149
|
|
|
4 => 'अप्रिल', |
150
|
|
|
5 => 'मई', |
151
|
|
|
6 => 'जून', |
152
|
|
|
7 => 'जुलाई', |
153
|
|
|
8 => 'अगष्ट', |
154
|
|
|
9 => 'सेप्टेम्बर', |
155
|
|
|
10 => 'अक्टोबर', |
156
|
|
|
11 => 'नवेम्बर', |
157
|
|
|
12 => 'दिसेम्बर', |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
private $dayOfWeekInNepali = [ |
161
|
|
|
1 => 'आइतवार', |
162
|
|
|
2 => 'सोमवार', |
163
|
|
|
3 => 'मङ्गलवार', |
164
|
|
|
4 => 'बुधवार', |
165
|
|
|
5 => 'बिहिवार', |
166
|
|
|
6 => 'शुक्रवार', |
167
|
|
|
7 => 'शनिवार', |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
private $numbersInNepali = [ |
171
|
|
|
0 => '०', |
172
|
|
|
1 => '१', |
173
|
|
|
2 => '२', |
174
|
|
|
3 => '३', |
175
|
|
|
4 => '४', |
176
|
|
|
5 => '५', |
177
|
|
|
6 => '६', |
178
|
|
|
7 => '७', |
179
|
|
|
8 => '८', |
180
|
|
|
9 => '९', |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
protected $normalMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
184
|
|
|
protected $leapMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
185
|
|
|
|
186
|
|
|
protected $dayOfWeek = 6; // 7 - 1, 0 for sunday, 6 for saturday |
187
|
|
|
|
188
|
|
|
public function getBSMonthInEnglish($month): string |
189
|
|
|
{ |
190
|
|
|
return $this->bsMonthInEnglish[$month]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getBSMonthInNepali($month): string |
194
|
|
|
{ |
195
|
|
|
return $this->bsMonthInNepali[$month]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getADMonthInNepali($month): string |
199
|
|
|
{ |
200
|
|
|
return $this->adMonthInNepali[$month]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getDayOfWeekInNepali($day): string |
204
|
|
|
{ |
205
|
|
|
return $this->dayOfWeekInNepali[$day]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getDayOfWeekInEnglish($day): string |
209
|
|
|
{ |
210
|
|
|
return $this->dayOfWeekInEnglish[$day]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function getNumbersInNepali($day): string |
214
|
|
|
{ |
215
|
|
|
return $this->numbersInNepali[$day]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function formattedNepaliNumber($value) |
219
|
|
|
{ |
220
|
|
|
$numbers = str_split($value); |
221
|
|
|
|
222
|
|
|
foreach ($numbers as $key => $number) { |
223
|
|
|
$numbers[$key] = $this->numbersInNepali[$number]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return implode('', $numbers); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Check if date range is in english. |
231
|
|
|
* |
232
|
|
|
* @param int $yy |
233
|
|
|
* @param int $mm |
234
|
|
|
* @param int $dd |
235
|
|
|
*/ |
236
|
|
|
protected function is_in_range_eng($yy, $mm, $dd) |
237
|
|
|
{ |
238
|
|
|
if ($yy < 1944 || $yy > 2033) { |
239
|
|
|
throw new \Exception('Supported only between 1944-2022'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ($mm < 1 || $mm > 12) { |
243
|
|
|
throw new \Exception('Error! month value can be between 1-12 only'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if ($dd < 1 || $dd > 31) { |
247
|
|
|
throw new \Exception('Error! day value can be between 1-31 only'); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Check if date is with in nepali data range. |
253
|
|
|
* |
254
|
|
|
* @param int $yy |
255
|
|
|
* @param int $mm |
256
|
|
|
* @param int $dd |
257
|
|
|
*/ |
258
|
|
|
protected function is_in_range_nep($yy, $mm, $dd) |
259
|
|
|
{ |
260
|
|
|
if ($yy < 2000 || $yy > 2089) { |
261
|
|
|
throw new \Exception('Supported only between 2000-2089'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
if ($mm < 1 || $mm > 12) { |
265
|
|
|
throw new \Exception('Error! month value can be between 1-12 only'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if ($dd < 1 || $dd > 32) { |
269
|
|
|
throw new \Exception('Error! day value can be between 1-31 only'); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Calculates wheather english year is leap year or not. |
275
|
|
|
* |
276
|
|
|
* @param int $year |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
|
|
protected function is_leap_year($year) |
280
|
|
|
{ |
281
|
|
|
$a = $year; |
282
|
|
|
if ($a % 100 == 0) { |
283
|
|
|
if ($a % 400 == 0) { |
284
|
|
|
return true; |
285
|
|
|
} else { |
286
|
|
|
return false; |
287
|
|
|
} |
288
|
|
|
} else { |
289
|
|
|
if ($a % 4 == 0) { |
290
|
|
|
return true; |
291
|
|
|
} else { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
protected function calculateTotalEnglishDays($year, $month, $day): int |
298
|
|
|
{ |
299
|
|
|
$total_ad_days = 0; |
300
|
|
|
$initial_english_year = 1944; |
301
|
|
|
|
302
|
|
|
for ($i = 0; $i < ($year - $initial_english_year); $i++) { |
303
|
|
|
if ($this->is_leap_year($initial_english_year + $i)) { |
304
|
|
|
for ($j = 0; $j < 12; $j++) { |
305
|
|
|
$total_ad_days += $this->leapMonths[$j]; |
306
|
|
|
} |
307
|
|
|
} else { |
308
|
|
|
for ($j = 0; $j < 12; $j++) { |
309
|
|
|
$total_ad_days += $this->normalMonths[$j]; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
for ($i = 0; $i < ($month - 1); $i++) { |
315
|
|
|
if ($this->is_leap_year($year)) { |
316
|
|
|
$total_ad_days += $this->leapMonths[$i]; |
317
|
|
|
} else { |
318
|
|
|
$total_ad_days += $this->normalMonths[$i]; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$total_ad_days += $day; |
323
|
|
|
|
324
|
|
|
return $total_ad_days; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Calculate total days from a reference Nepali date to the specified Nepali date. |
329
|
|
|
* |
330
|
|
|
* @param int $year Nepali year |
331
|
|
|
* @param int $month Nepali month (1-12) |
332
|
|
|
* @param int $day Nepali day |
333
|
|
|
* @return int Total number of days |
334
|
|
|
*/ |
335
|
|
|
protected function calculateTotalNepaliDays($year, $month, $day): int |
336
|
|
|
{ |
337
|
|
|
$total_bs_days = 0; |
338
|
|
|
$initial_nepali_year = 2000; |
339
|
|
|
|
340
|
|
|
// Add days for complete years |
341
|
|
|
for ($i = $initial_nepali_year; $i < $year; $i++) { |
342
|
|
|
for ($j = 1; $j <= 12; $j++) { |
343
|
|
|
$yearIndex = $i - $initial_nepali_year; |
344
|
|
|
$total_bs_days += $this->calendarData[$yearIndex][$j]; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// Add days for complete months in the target year |
349
|
|
|
$yearIndex = $year - $initial_nepali_year; |
350
|
|
|
for ($j = 1; $j < $month; $j++) { |
351
|
|
|
$total_bs_days += $this->calendarData[$yearIndex][$j]; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
// Add remaining days in the final month |
355
|
|
|
$total_bs_days += $day; |
356
|
|
|
|
357
|
|
|
return $total_bs_days; |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|