1
|
|
|
<?php namespace OpenCafe\Tools; |
2
|
|
|
|
3
|
|
|
/************************************************************ |
4
|
|
|
* Convert Calendars types together |
5
|
|
|
************************************************************ |
6
|
|
|
* |
7
|
|
|
* @since Oct 27, 2015 |
8
|
|
|
* |
9
|
|
|
*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |
10
|
|
|
*/ |
11
|
|
|
class Convert |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var integer |
16
|
|
|
*/ |
17
|
|
|
protected $year; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var integer |
21
|
|
|
*/ |
22
|
|
|
protected $month; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var $day |
26
|
|
|
*/ |
27
|
|
|
protected $day; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DateTime |
31
|
|
|
*/ |
32
|
|
|
public $date_time; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $config; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var integer |
41
|
|
|
*/ |
42
|
|
|
protected $leap; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var integer |
46
|
|
|
*/ |
47
|
|
|
protected $temp_day; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var DateTime |
51
|
|
|
*/ |
52
|
|
|
protected $date; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $calendar_file; |
58
|
|
|
|
59
|
|
|
/************************************************************ |
60
|
|
|
* Convert class constructor |
61
|
|
|
************************************************************ |
62
|
|
|
* |
63
|
|
|
* @since Oct 27, 2015 |
64
|
|
|
* |
65
|
|
|
*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |
66
|
|
|
*/ |
67
|
|
|
public function __construct($date_time = null) |
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
if ($date_time !== null) { |
71
|
|
|
$this->date_time = $date_time; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->config = include __DIR__.'/Config.php'; |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function from($calendar) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
|
81
|
|
|
$this->calendar_file = include __DIR__.'/CalendarSettings/' . ucfirst($calendar) . '.php'; |
82
|
|
|
|
83
|
|
|
return $this->calendar_file[ 'convert_from' ]( $this->date_time ); |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/************************************************************ |
88
|
|
|
* Convert to specific calendar |
89
|
|
|
************************************************************ |
90
|
|
|
* |
91
|
|
|
* @since Oct 26, 2015 |
92
|
|
|
* |
93
|
|
|
*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function to($calendar) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
|
98
|
|
|
$this->calendar_file = include __DIR__.'/CalendarSettings/' . ucfirst($calendar) . '.php'; |
99
|
|
|
|
100
|
|
|
return $this->calendar_file[ 'convert_to' ]( $this->date_time ); |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
*convert jalali year to gregorian year |
106
|
|
|
* |
107
|
|
|
* @since Oct, 16 2015 |
108
|
|
|
* @return object |
109
|
|
|
*/ |
110
|
|
|
public function jalaliToGregorian($date_time) |
111
|
|
|
{ |
112
|
|
|
|
113
|
|
|
$this->config = include __DIR__.'/Jalali.php'; |
114
|
|
|
|
115
|
|
|
$this->date_time = $date_time; |
116
|
|
|
|
117
|
|
|
$this->year = $this->date_time->format('Y'); |
118
|
|
|
|
119
|
|
|
$this->month = $this->date_time->format('m'); |
120
|
|
|
|
121
|
|
|
$this->day = $this->date_time->format('d'); |
122
|
|
|
|
123
|
|
|
$days_of_year = 0; |
124
|
|
|
|
125
|
|
|
foreach ($this->config[ 'month_days_number' ] as $month => $value) { |
126
|
|
|
if ($this->month > $month) { |
127
|
|
|
$days_of_year += $value; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$days_of_year += $this->day; |
132
|
|
|
|
133
|
|
|
$days_of_leap_years = intval(( ( $this->year - 1 ) / 4 )); |
134
|
|
|
|
135
|
|
|
$days_of_jalali_years = ( ( ( $this->year - 1 ) * 365 ) + $days_of_year + $days_of_leap_years ); |
136
|
|
|
|
137
|
|
|
$days_of_gregorain_years = $days_of_jalali_years + 226899; |
138
|
|
|
|
139
|
|
|
if ($this->month < 10) { |
140
|
|
|
$days_of_gregorain_years = $days_of_gregorain_years - intval(( ( $this->year + 621 ) / 4 )); |
141
|
|
|
} elseif (( ( 10 == $this->month ) && ( $this->day > 10 ) ) || ( $this->month > 10 )) { |
142
|
|
|
$days_of_gregorain_years = $days_of_gregorain_years - intval(( ( $this->year + 622 ) / 4 )); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$gregorian_month = ( $days_of_gregorain_years % 365 ); |
146
|
|
|
|
147
|
|
|
$gregorian_year = intval($days_of_gregorain_years / 365) + 1; |
148
|
|
|
|
149
|
|
|
$this->config = include __DIR__.'/Gregorian.php'; |
150
|
|
|
|
151
|
|
|
foreach ($this->config[ 'month_days_number' ] as $month => $value) { |
152
|
|
|
if ($gregorian_month < $value) { |
153
|
|
|
break; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$gregorian_month -= $value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$gregorian_day = $gregorian_month; |
160
|
|
|
|
161
|
|
|
$gregorian_month = $month; |
|
|
|
|
162
|
|
|
|
163
|
|
|
$this->date_time->setDate($gregorian_year, $gregorian_month, $gregorian_day); |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
return $this->date_time; |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
*convert jalali year to hijri year |
172
|
|
|
* |
173
|
|
|
* @since Oct, 17 2015 |
174
|
|
|
* @return object |
175
|
|
|
*/ |
176
|
|
|
public function jalaliToHijri($date_time) |
177
|
|
|
{ |
178
|
|
|
|
179
|
|
|
$this->date_time = $date_time; |
180
|
|
|
|
181
|
|
|
$this->year = $this->date_time->format('Y'); |
182
|
|
|
|
183
|
|
|
$this->month = $this->date_time->format('n'); |
184
|
|
|
|
185
|
|
|
$this->day = $this->date_time->format('d'); |
186
|
|
|
|
187
|
|
|
$this->temp_day = 0 ; |
188
|
|
|
|
189
|
|
|
$this->config = include __DIR__.'/Jalali.php'; |
190
|
|
|
|
191
|
|
|
for ($i = 1; $i < $this->month; $i++) { |
192
|
|
|
$this->temp_day += $this->config['month_days_number'][$i]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->temp_day += $this->day; |
196
|
|
|
|
197
|
|
|
$this->leap = new Leap($this->year); |
|
|
|
|
198
|
|
|
|
199
|
|
|
if ($this->leap->get() && $this->month > 11) { |
200
|
|
|
$this->temp_day++; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$_year = ( ( ( ( ( $this->year - 1 ) * 365.2422 ) + $this->temp_day ) - 119) / 354.3670 ) + 1; |
204
|
|
|
|
205
|
|
|
$_year = explode('.', $_year); |
206
|
|
|
|
207
|
|
|
$this->year = $_year[0]; |
208
|
|
|
|
209
|
|
|
$_month = $_year[1]; |
210
|
|
|
|
211
|
|
|
$var_temp = '0.0'; |
212
|
|
|
|
213
|
|
|
for ($i = strlen($_month); $i > 2; $i--) { |
214
|
|
|
$var_temp .= '0'; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$var_temp .= '1'; |
218
|
|
|
|
219
|
|
|
$_month = $_month * $var_temp ; |
220
|
|
|
|
221
|
|
|
$_month = ( $_month * 12 ) + 1; |
222
|
|
|
|
223
|
|
|
$_month = explode('.', $_month); |
224
|
|
|
|
225
|
|
|
$this->month = $_month[0]; |
226
|
|
|
|
227
|
|
|
$_day = $_month[1]; |
228
|
|
|
|
229
|
|
|
$var_temp = '0.0'; |
230
|
|
|
|
231
|
|
|
for ($i = strlen($_day); $i > 2; $i--) { |
232
|
|
|
$var_temp .= '0' ; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$var_temp .= '1'; |
236
|
|
|
|
237
|
|
|
$_day = $_day * $var_temp; |
238
|
|
|
|
239
|
|
|
$_day = ( $_day * 29.530 ); |
240
|
|
|
|
241
|
|
|
$_day = explode('.', $_day); |
242
|
|
|
|
243
|
|
|
$this->day = $_day[0]; |
244
|
|
|
|
245
|
|
|
$this->date_time->setDate($this->year, $this->month, $this->day); |
246
|
|
|
|
247
|
|
|
return $this->date_time; |
248
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
*convert hijri year to jalali year |
253
|
|
|
* |
254
|
|
|
* @since Oct, 17 2015 |
255
|
|
|
* @return object |
256
|
|
|
*/ |
257
|
|
View Code Duplication |
public function hijriToJalali($date_time) |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
|
260
|
|
|
$this->date_time = $date_time; |
261
|
|
|
|
262
|
|
|
$this->year = $this->date_time->format('Y'); |
263
|
|
|
|
264
|
|
|
$this->month = $this->date_time->format('m'); |
265
|
|
|
|
266
|
|
|
$this->day = $this->date_time->format('d'); |
267
|
|
|
|
268
|
|
|
$days_of_year = 0; |
269
|
|
|
|
270
|
|
|
$this->config = include __DIR__.'/Hijri.php'; |
271
|
|
|
|
272
|
|
|
foreach ($this->config[ 'month_days_number' ] as $month => $value) { |
273
|
|
|
if ($this->month > $month) { |
274
|
|
|
$days_of_year += $value; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$days_of_year += $this->day; |
279
|
|
|
|
280
|
|
|
$days_of_leap_years = intval(( ( $this->year - 1 ) / 3 )); |
281
|
|
|
|
282
|
|
|
$days_of_hijri_years = ( ( ( $this->year - 1 ) * 354 ) + $days_of_year + $days_of_leap_years ); |
283
|
|
|
|
284
|
|
|
$days_of_jalali_years = $days_of_hijri_years + 179; |
285
|
|
|
|
286
|
|
|
$days_of_jalali_years = $days_of_jalali_years - intval(( ( $this->year - 43 ) / 4 )); |
287
|
|
|
|
288
|
|
|
$jalali_month = ( $days_of_jalali_years % 365 ); |
289
|
|
|
|
290
|
|
|
$jalali_year = intval($days_of_jalali_years / 365) + 1; |
291
|
|
|
|
292
|
|
|
$this->config = include __DIR__.'/Jalali.php'; |
293
|
|
|
|
294
|
|
|
foreach ($this->config[ 'month_days_number' ] as $month => $value) { |
295
|
|
|
if ($jalali_month < $value) { |
296
|
|
|
break; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$jalali_month -= $value; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$jalali_day = $jalali_month; |
303
|
|
|
|
304
|
|
|
$jalali_month = $month; |
|
|
|
|
305
|
|
|
|
306
|
|
|
$this->date_time->setDate($jalali_year, $jalali_month, $jalali_day); |
307
|
|
|
|
308
|
|
|
return $this->date_time; |
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* convert hijri year to gregorian year |
314
|
|
|
* |
315
|
|
|
* @since Oct, 17 2015 |
316
|
|
|
* @return object |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
public function hijriToGregorian($date_time) |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
|
321
|
|
|
$this->date_time = $date_time; |
322
|
|
|
|
323
|
|
|
$this->year = $this->date_time->format('Y'); |
324
|
|
|
|
325
|
|
|
$this->month = $this->date_time->format('m'); |
326
|
|
|
|
327
|
|
|
$this->day = $this->date_time->format('d'); |
328
|
|
|
|
329
|
|
|
$days_of_year = 0; |
330
|
|
|
|
331
|
|
|
$this->config = include __DIR__.'/Hijri.php'; |
332
|
|
|
|
333
|
|
|
foreach ($this->config[ 'month_days_number' ] as $month => $value) { |
334
|
|
|
if ($this->month > $month) { |
335
|
|
|
$days_of_year += $value; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
$days_of_year += $this->day; |
340
|
|
|
|
341
|
|
|
$days_of_leap_years = intval(( ( $this->year - 1 ) / 3 )); |
342
|
|
|
|
343
|
|
|
$days_of_hijri_years = ( ( ( $this->year - 1 ) * 354 ) + $days_of_year + $days_of_leap_years ); |
344
|
|
|
|
345
|
|
|
$days_of_gregorain_years = $days_of_hijri_years + 227078; |
346
|
|
|
|
347
|
|
|
$days_of_gregorain_years = $days_of_gregorain_years - intval(( ( $this->year + 578 ) / 4 )); |
348
|
|
|
|
349
|
|
|
$gregorian_month = ( $days_of_gregorain_years % 365 ); |
350
|
|
|
|
351
|
|
|
$gregorian_year = intval($days_of_gregorain_years / 365) + 1; |
352
|
|
|
|
353
|
|
|
$this->config = include __DIR__.'/Gregorian.php'; |
354
|
|
|
|
355
|
|
|
foreach ($this->config[ 'month_days_number' ] as $month => $value) { |
356
|
|
|
if ($gregorian_month < $value) { |
357
|
|
|
break; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
$gregorian_month -= $value; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$gregorian_day = $gregorian_month; |
364
|
|
|
|
365
|
|
|
$gregorian_month = $month; |
|
|
|
|
366
|
|
|
|
367
|
|
|
$this->date_time->setDate($gregorian_year, $gregorian_month, $gregorian_day); |
368
|
|
|
|
369
|
|
|
return $this->date_time; |
370
|
|
|
|
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.