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