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
|
|
|
$this->persian_month = $this->config['month']['persian']; |
|
|
|
|
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/************************************************************ |
81
|
|
|
* Convert to specific calendar |
82
|
|
|
************************************************************ |
83
|
|
|
* |
84
|
|
|
* @since Oct 26, 2015 |
85
|
|
|
* |
86
|
|
|
*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |
87
|
|
|
*/ |
88
|
|
|
public function to( $calendar ) { |
89
|
|
|
|
90
|
|
|
$this->calendar_file = include( 'CalendarSettings/' . ucfirst( $calendar ) . '.php' ); |
91
|
|
|
|
92
|
|
|
return $this->calendar_file[ 'convert_to' ]( $this->date_time ); |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
*convert shamsi year to gregorian year |
98
|
|
|
* @since Oct, 16 2015 |
99
|
|
|
* @return object |
100
|
|
|
*/ |
101
|
|
|
public function shamsiToGregorian( $date_time ) { |
102
|
|
|
|
103
|
|
|
$this->date_time = $date_time; |
104
|
|
|
|
105
|
|
|
$this->year = $this->date_time->format('Y'); |
106
|
|
|
|
107
|
|
|
$this->month = $this->date_time->format('m'); |
108
|
|
|
|
109
|
|
|
$this->day = $this->date_time->format('d'); |
110
|
|
|
|
111
|
|
|
$days_of_year = 0; |
112
|
|
|
|
113
|
|
|
foreach ( $this->config['shamsi_month_days'] as $month => $value ) { |
114
|
|
|
|
115
|
|
|
if( $this->month > $month ) $days_of_year += $value; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$days_of_year += $this->day; |
120
|
|
|
|
121
|
|
|
$days_of_leap_years = intval( ( ( $this->year - 1 ) / 4 ) ); |
122
|
|
|
|
123
|
|
|
$days_of_shamsi_years = ( ( ( $this->year - 1 ) * 365 ) + $days_of_year + $days_of_leap_years ); |
124
|
|
|
|
125
|
|
|
$days_of_gregorain_years = $days_of_shamsi_years + 226899; |
126
|
|
|
|
127
|
|
|
if ( $this->month < 10 ) { |
128
|
|
|
|
129
|
|
|
$days_of_gregorain_years = $days_of_gregorain_years - intval( ( ( $this->year + 621 ) / 4 ) ); |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
elseif ( ( ( 10 == $this->month ) && ( $this->day > 10 ) ) || ( $this->month > 10 ) ) { |
134
|
|
|
|
135
|
|
|
$days_of_gregorain_years = $days_of_gregorain_years - intval( ( ( $this->year + 622 ) / 4 ) ); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$gregorian_month = ( $days_of_gregorain_years % 365 ); |
140
|
|
|
|
141
|
|
|
$gregorian_year = intval( $days_of_gregorain_years / 365 ) + 1; |
142
|
|
|
|
143
|
|
|
foreach ($this->config['gregorian_month_days'] as $month => $value) { |
144
|
|
|
|
145
|
|
|
if ( $gregorian_month < $value ) break; |
146
|
|
|
|
147
|
|
|
$gregorian_month -= $value; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$gregorian_day = $gregorian_month; |
151
|
|
|
|
152
|
|
|
$gregorian_month = $month; |
|
|
|
|
153
|
|
|
|
154
|
|
|
$this->date_time->setDate( $gregorian_year, $gregorian_month, $gregorian_day ); |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
return $this->date_time; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
*convert shamsi year to ghamari year |
163
|
|
|
* @since Oct, 17 2015 |
164
|
|
|
* @return object |
165
|
|
|
*/ |
166
|
|
|
public function shamsiToGhamari( $date_time ) { |
167
|
|
|
|
168
|
|
|
$this->date_time = $date_time; |
169
|
|
|
|
170
|
|
|
$this->year = $this->date_time->format('Y'); |
171
|
|
|
|
172
|
|
|
$this->month = $this->date_time->format('n'); |
173
|
|
|
|
174
|
|
|
$this->day = $this->date_time->format('d'); |
175
|
|
|
|
176
|
|
|
$this->temp_day = 0 ; |
177
|
|
|
|
178
|
|
|
for ( $i = 1 ; $i < $this->month ; $i++ ) { |
179
|
|
|
|
180
|
|
|
$this->temp_day += $this->config['shamsi_month_days'][$i]; |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$this->temp_day += $this->day; |
185
|
|
|
|
186
|
|
|
$this->leap = new Leap( $this->year ); |
|
|
|
|
187
|
|
|
|
188
|
|
|
if( $this->leap->get() && $this->month > 11 ) $this->temp_day++; |
189
|
|
|
|
190
|
|
|
$_year = ( ( ( ( ( $this->year - 1 ) * 365.2422 ) + $this->temp_day ) - 119) / 354.3670 ) + 1; |
191
|
|
|
|
192
|
|
|
$_year = explode( '.', $_year ); |
193
|
|
|
|
194
|
|
|
$this->year = $_year[0]; |
195
|
|
|
|
196
|
|
|
$_month = $_year[1]; |
197
|
|
|
|
198
|
|
|
$var_temp = '0.0'; |
199
|
|
|
|
200
|
|
|
for ( $i = strlen( $_month ); $i > 2; $i-- ) { |
201
|
|
|
|
202
|
|
|
$var_temp .= '0'; |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$var_temp .= '1'; |
207
|
|
|
|
208
|
|
|
$_month = $_month * $var_temp ; |
209
|
|
|
|
210
|
|
|
$_month = ( $_month * 12 ) + 1; |
211
|
|
|
|
212
|
|
|
$_month = explode( '.', $_month ); |
213
|
|
|
|
214
|
|
|
$this->month = $_month[0]; |
215
|
|
|
|
216
|
|
|
$_day = $_month[1]; |
217
|
|
|
|
218
|
|
|
$var_temp = '0.0'; |
219
|
|
|
|
220
|
|
|
for ( $i = strlen( $_day ); $i > 2; $i-- ) { |
221
|
|
|
|
222
|
|
|
$var_temp .= '0' ; |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$var_temp .= '1'; |
227
|
|
|
|
228
|
|
|
$_day = $_day * $var_temp; |
229
|
|
|
|
230
|
|
|
$_day = ( $_day * 29.530 ); |
231
|
|
|
|
232
|
|
|
$_day = explode( '.', $_day ); |
233
|
|
|
|
234
|
|
|
$this->day = $_day[0]; |
235
|
|
|
|
236
|
|
|
$this->date_time->setDate( $this->year, $this->month, $this->day ); |
237
|
|
|
|
238
|
|
|
return $this->date_time; |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
*convert ghamari year to shamsi year |
244
|
|
|
* @since Oct, 17 2015 |
245
|
|
|
* @return object |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
public function ghamariToShamsi( $date_time ) { |
|
|
|
|
248
|
|
|
|
249
|
|
|
$this->date_time = $date_time; |
250
|
|
|
|
251
|
|
|
$this->year = $this->date_time->format('Y'); |
252
|
|
|
|
253
|
|
|
$this->month = $this->date_time->format('m'); |
254
|
|
|
|
255
|
|
|
$this->day = $this->date_time->format('d'); |
256
|
|
|
|
257
|
|
|
$days_of_year = 0; |
258
|
|
|
|
259
|
|
|
foreach ( $this->config['islamic_month_days'] as $month => $value ) { |
260
|
|
|
|
261
|
|
|
if( $this->month > $month ) $days_of_year += $value; |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$days_of_year += $this->day; |
266
|
|
|
|
267
|
|
|
$days_of_leap_years = intval( ( ( $this->year - 1 ) / 3 ) ); |
268
|
|
|
|
269
|
|
|
$days_of_ghamari_years = ( ( ( $this->year - 1 ) * 354 ) + $days_of_year + $days_of_leap_years ); |
270
|
|
|
|
271
|
|
|
$days_of_shamsi_years = $days_of_ghamari_years + 179; |
272
|
|
|
|
273
|
|
|
$days_of_shamsi_years = $days_of_shamsi_years - intval( ( ( $this->year - 43 ) / 4 ) ); |
274
|
|
|
|
275
|
|
|
$shamsi_month = ( $days_of_shamsi_years % 365 ); |
276
|
|
|
|
277
|
|
|
$shamsi_year = intval( $days_of_shamsi_years / 365 ) + 1; |
278
|
|
|
|
279
|
|
|
foreach ($this->config['shamsi_month_days'] as $month => $value) { |
280
|
|
|
|
281
|
|
|
if ( $shamsi_month < $value ) break; |
282
|
|
|
|
283
|
|
|
$shamsi_month -= $value; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$shamsi_day = $shamsi_month; |
287
|
|
|
|
288
|
|
|
$shamsi_month = $month; |
|
|
|
|
289
|
|
|
|
290
|
|
|
$this->date_time->setDate( $shamsi_year, $shamsi_month, $shamsi_day ); |
291
|
|
|
|
292
|
|
|
return $this->date_time; |
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* convert ghamari year to gregorian year |
298
|
|
|
* @since Oct, 17 2015 |
299
|
|
|
* @return object |
300
|
|
|
*/ |
301
|
|
View Code Duplication |
public function ghamariToGregorian( $date_time ) { |
|
|
|
|
302
|
|
|
|
303
|
|
|
$this->date_time = $date_time; |
304
|
|
|
|
305
|
|
|
$this->year = $this->date_time->format('Y'); |
306
|
|
|
|
307
|
|
|
$this->month = $this->date_time->format('m'); |
308
|
|
|
|
309
|
|
|
$this->day = $this->date_time->format('d'); |
310
|
|
|
|
311
|
|
|
$days_of_year = 0; |
312
|
|
|
|
313
|
|
|
foreach ( $this->config['islamic_month_days'] as $month => $value ) { |
314
|
|
|
|
315
|
|
|
if( $this->month > $month ) $days_of_year += $value; |
316
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$days_of_year += $this->day; |
320
|
|
|
|
321
|
|
|
$days_of_leap_years = intval( ( ( $this->year - 1 ) / 3 ) ); |
322
|
|
|
|
323
|
|
|
$days_of_ghamari_years = ( ( ( $this->year - 1 ) * 354 ) + $days_of_year + $days_of_leap_years ); |
324
|
|
|
|
325
|
|
|
$days_of_gregorain_years = $days_of_ghamari_years + 227078; |
326
|
|
|
|
327
|
|
|
$days_of_gregorain_years = $days_of_gregorain_years - intval( ( ( $this->year + 578 ) / 4 ) ); |
328
|
|
|
|
329
|
|
|
$gregorian_month = ( $days_of_gregorain_years % 365 ); |
330
|
|
|
|
331
|
|
|
$gregorian_year = intval( $days_of_gregorain_years / 365 ) + 1; |
332
|
|
|
|
333
|
|
|
foreach ($this->config['gregorian_month_days'] as $month => $value) { |
334
|
|
|
|
335
|
|
|
if ( $gregorian_month < $value ) break; |
336
|
|
|
|
337
|
|
|
$gregorian_month -= $value; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
$gregorian_day = $gregorian_month; |
341
|
|
|
|
342
|
|
|
$gregorian_month = $month; |
|
|
|
|
343
|
|
|
|
344
|
|
|
$this->date_time->setDate( $gregorian_year, $gregorian_month, $gregorian_day ); |
345
|
|
|
|
346
|
|
|
return $this->date_time; |
347
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
} |
351
|
|
|
?> |
|
|
|
|
352
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: