Completed
Push — master ( d5f8b0...0fe70c )
by mehdi
02:20
created

src/CalendarSettings/Ghamari.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
 return array (
4
5
 /************************************************************
6
  *                        Convert to
7
  ************************************************************
8
  *
9
  * Convert algorith to convert Gregorian to Jalali calerndar
10
  *
11
  *\_________________________________________________________/
12
  */
13
   'convert_to' => function( $date_time ) {
14
15
     $config = include( 'Shamsi.php' );
16
17
     $date_time = Datium\Datium::create( $date_time )->to( 'shamsi' )->object();
18
19
     $year = $date_time->format('Y');
20
21
     $month = $date_time->format('n');
22
23
     $day = $date_time->format('d');
24
25
     $temp_day = 0 ;
26
27
     for ( $i = 1 ; $i < $month ; $i++ ) {
28
29
         $temp_day += $config[ 'month_days_number' ][ $i ];
30
31
       }
32
33
      $temp_day += $day;
34
35
      $leap = new Datium\Tools\Leap( $year );
36
37
     if( $leap->get() && $month > 11 ) $temp_day++;
38
39
     $_year = ( ( ( ( ( $year - 1 ) * 365.2422 ) + $temp_day ) - 119) / 354.3670 ) + 1;
40
41
     $_year = explode( '.', $_year );
42
43
     $year = $_year[0];
44
45
     $_month = $_year[1];
46
47
      $var_temp = '0.0';
48
49
       for ( $i = strlen( $_month ); $i > 2; $i-- ) {
50
51
         $var_temp .= '0';
52
53
      }
54
55
      $var_temp .= '1';
56
57
     $_month = $_month * $var_temp ;
58
59
     $_month = ( $_month * 12 ) + 1;
60
61
     $_month = explode( '.', $_month );
62
63
     $month = $_month[0];
64
65
     $_day = $_month[1];
66
67
     $var_temp = '0.0';
68
69
     for ( $i = strlen( $_day );  $i > 2;  $i-- ) {
70
71
        $var_temp .= '0' ;
72
73
     }
74
75
     $var_temp .= '1';
76
77
     $_day = $_day * $var_temp;
78
79
     $_day = ( $_day * 29.530 );
80
81
     $_day = explode( '.', $_day );
82
83
     $day = $_day[0];
84
85
    $date_time->setDate( $year, $month, $day );
86
87
    return $date_time;
88
89
   },
90
91
   /************************************************************
92
    *                        Convert From
93
    ************************************************************
94
    *
95
    * Convert algorith to convert Jalali to Gregorian calerndar
96
    *
97
    *\_________________________________________________________/
98
    */
99
   'convert_from' => function( $date_time ) {
100
101
     $config = include('Ghamari.php');
102
103
     $year = $date_time->format('Y');
104
105
     $month = $date_time->format('m');
106
107
     $day = $date_time->format('d');
108
109
     $days_of_year = 0;
110
111
     foreach ( $config['month_days_number'] as $month => $value ) {
112
113
       if( $month > $month ) $days_of_year += $value;
114
115
     }
116
117
     $days_of_year += $day;
118
119
     $days_of_leap_years =  intval( ( ( $year - 1 ) / 3 )  );
120
121
     $days_of_ghamari_years = ( ( ( $year - 1 ) * 354 ) + $days_of_year + $days_of_leap_years );
122
123
     $days_of_gregorain_years = $days_of_ghamari_years + 227078;
124
125
     $days_of_gregorain_years = $days_of_gregorain_years - intval( ( ( $year + 578 ) / 4 ) );
126
127
     $gregorian_month = ( $days_of_gregorain_years % 365 );
128
129
     $gregorian_year = intval( $days_of_gregorain_years / 365 ) + 1;
130
131
     $config = include('Gregorian.php');
132
133
     foreach ($config [ 'month_days_number' ] as $month => $value) {
134
135
       if ( $gregorian_month < $value ) {
136
137
         break;
138
       }
139
140
         $gregorian_month -= $value;
141
     }
142
143
       $gregorian_day = $gregorian_month;
144
145
       $gregorian_month = $month;
146
147
       $date_time->setDate( $gregorian_year, $gregorian_month, $gregorian_day );
148
149
      return $date_time;
150
151
152
   },
153
154
   /************************************************************
155
    *               Shorthand for jalali calendar
156
    ************************************************************
157
    *
158
    * Jalali calendar shorthand
159
    *
160
    *\_________________________________________________________/
161
    */
162
   'shorthand' => 'sh',
163
164
   /************************************************************
165
    *                        Month's name
166
    ************************************************************
167
    *
168
    * Jalali month name
169
    *
170
    *\_________________________________________________________/
171
    */
172
   'month' => array (
173
174
        'Muharram',
175
176
        'Safar',
177
178
        'Rabi I',
179
180
        'Rabi II',
181
182
        'Jumada I',
183
184
        'Jumada II',
185
186
        'Rajab',
187
188
        'Shaban',
189
190
        'Ramadan',
191
192
        'Shawwal',
193
194
        'Dhu al_Qadah',
195
196
        'Dhu al_Hijjah'
197
198
     ),
199
200
   /************************************************************
201
    *                        Days of Week
202
    ************************************************************
203
    *
204
    * Here is week days on jalali calendar, offset 0 is first
205
    * day of week and offset 6 is the last one.
206
    *
207
    *\_________________________________________________________/
208
    */
209
   'days_of_week' => array (
210
         'al-Aḥad',
211
         'al-Ithnayn',
212
         'ath-Thulatha\'',
213
         'al-Arbi\'a',
214
         'al-Khamees',
215
         'al-Jumu\'ah',
216
         'as-Sabt',
217
218
   ),
219
220
   'month_days_number' => array(     1 => 30,
221
                                     2 => 29,
222
                                     3 => 30,
223
                                     4 => 30,
224
                                     5 => 29,
225
                                     6 => 29,
226
                                     7 => 30,
227
                                     8 => 29,
228
                                     9 => 30,
229
                                     10 => 29,
230
                                     11 => 30,
231
                                     12 => 30 ),
232
233 View Code Duplication
     'day_of_year' => function( $date_time ) {
234
235
       $result = null;
236
237
       $config = include( 'Ghamari.php' );
238
239
       $month = $date_time->format('n');
240
241
       $day = $date_time->format('d');
242
243
       foreach( $config['month_days_number'] as $_month => $value ) {
244
245
         if ( $_month < $month ) {
246
247
           $result += $value;
248
249
         }
250
251
       }
252
253
       $result += $day;
254
255
       return $result + 1;
256
257
     },
258
259 View Code Duplication
     'day_of_week' => function( $date_time ) {
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
260
261
       $configGhamari = include(   'Ghamari.php' );
262
263
       $configGregorian = include( 'Gregorian.php' );
264
265
       $day = $date_time->format('l');
266
267
       $day = str_replace( $configGregorian['days_of_week'], $configGhamari['days_of_week'], $day);
268
269
       foreach ( $configGhamari['days_of_week'] as $key => $value ) {
270
271
         if( $value == $day ) {
272
273
           return $key += 1;
274
275
         }
276
277
       }
278
279
     },
280
281
     /************************************************************
282
      *                       Leap year
283
      ************************************************************
284
      *
285
      * Leap Year formula on jalali calendar
286
      *
287
      *\_________________________________________________________/
288
      */
289
     'leap_year' => null,
290
291
   /************************************************************
292
    *                        Weekend
293
    ************************************************************
294
    *
295
    * Jalali weekend
296
    *
297
    *\_________________________________________________________/
298
    */
299
   'weekend' => array( 'friday' )
300
301
  );
302