Parsi Date - Version 2.3.4

Version Description

  • Fix diagnosis leap year function ( bn_parsidate::IsLeapYear() )
Download this release

Release Info

Developer man4toman
Plugin Icon Parsi Date
Version 2.3.4
Comparing to
See all releases

Code changes from version 2.3.3 to 2.3.4

Files changed (4) hide show
  1. .gitignore +0 -1
  2. includes/parsidate.php +380 -352
  3. readme.txt +4 -1
  4. wp-parsidate.php +1 -1
.gitignore DELETED
@@ -1 +0,0 @@
1
- .idea/
 
includes/parsidate.php CHANGED
@@ -6,370 +6,396 @@
6
  * @package WP-Parsidate
7
  * @subpackage DateConversation
8
  */
 
9
  /*Special thanks to :
10
  Reza Gholampanahi for convert function*/
11
 
12
- class bn_parsidate {
13
- protected static $instance;
14
- public $persian_month_names = array(
15
- '',
16
- 'فروردین',
17
- 'اردیبهشت',
18
- 'خرداد',
19
- 'تیر',
20
- 'مرداد',
21
- 'شهریور',
22
- 'مهر',
23
- 'آبان',
24
- 'آذر',
25
- 'دی',
26
- 'بهمن',
27
- 'اسفند'
28
- );
29
- public $persian_short_month_names = array(
30
- '',
31
- 'فرو',
32
- 'ارد',
33
- 'خرد',
34
- 'تیر',
35
- 'مرد',
36
- 'شهر',
37
- 'مهر',
38
- 'آبا',
39
- 'آذر',
40
- 'دی',
41
- 'بهم',
42
- 'اسف'
43
- );
44
- public $sesson = array( 'بهار', 'تابستان', 'پاییز', 'زمستان' );
 
 
 
 
 
 
 
 
 
 
45
 
46
- public $persian_day_names = array( 'یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه', 'شنبه' );
47
- public $persian_day_small = array( 'ی', 'د', 'س', 'چ', 'پ', 'ج', 'ش' );
 
 
 
 
 
 
 
48
 
49
- public $j_days_in_month = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
50
- private $j_days_sum_month = array( 0, 0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336 );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- private $g_days_sum_month = array( 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 );
 
53
 
 
 
54
 
55
- /**
56
- * Constructor
57
- */
58
- function __construct() {
59
- if ( $this->IsLeapYear( $this->persian_date( "Y", time(), "eng" ) ) ) {
60
- $this->j_days_in_month[11] = 30;
61
- }
62
- }
 
 
 
 
 
 
63
 
64
- /**
65
- * bn_parsidate::IsLeapYear()
66
- * check year is leap
67
- *
68
- * @param mixed $year
69
- *
70
- * @return boolean
71
- */
72
- private function IsLeapYear( $year ) {
73
- if ( ( ( $year % 4 ) == 0 && ( $year % 100 ) != 0 ) || ( ( $year % 400 ) == 0 ) && ( $year % 100 ) == 0 ) {
74
- return true;
75
- } else {
76
- return false;
77
- }
78
- }
79
 
80
- /**
81
- * bn_parsidate::persian_date()
82
- * convert gregorian datetime to persian datetime
83
- *
84
- * @param mixed $format
85
- * @param string $date
86
- * @param string $lang
87
- *
88
- * @return datetime
89
- */
90
- public function persian_date( $format, $date = 'now', $lang = 'per' ) {
91
- $j_days_in_month = array( 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365 );
92
- $timestamp = is_numeric( $date ) && (int) $date == $date ? $date : strtotime( $date );
93
 
94
- $date = getdate( $timestamp );
95
- list( $date['year'], $date['mon'], $date['mday'] ) = self::gregorian_to_persian( $date['year'], $date['mon'], $date['mday'] );
96
- $date['mon'] = (int) $date['mon'];
97
- $date['mday'] = (int) $date['mday'];
98
- $out = '';
99
- for ( $i = 0; $i < strlen( $format ); $i ++ ) {
100
- Switch ( $format[ $i ] ) {
101
- //day
102
- case 'd':
103
- $out .= ( $date['mday'] < 10 ) ? '0' . $date['mday'] : $date['mday'];
104
- break;
105
- case 'D':
106
- $out .= $this->persian_day_small[ $date['wday'] ];
107
- break;
108
- case'l':
109
- $out .= $this->persian_day_names[ $date['wday'] ];
110
- break;
111
- case 'j':
112
- $out .= $date['mday'];
113
- break;
114
- case'N':
115
- $out .= $this->week_day( $date['wday'] ) + 1;
116
- break;
117
- case'w':
118
- $out .= $this->week_day( $date['wday'] );
119
- break;
120
- case'z':
121
- $out .= $this->j_days_in_month[ $date['mon'] ] + $date['mday'];
122
- break;
123
- //week
124
- case'W':
125
- $yday = $this->j_days_sum_month[ $date['mon'] - 1 ] + $date['mday'];
126
- $out .= intval( $yday / 7 );
127
- break;
128
- //month
129
- case'f':
130
- $mon = $date['mon'];
131
- switch ( $mon ) {
132
- case( $mon < 4 ):
133
- $out .= $this->sesson[0];
134
- break;
135
- case( $mon < 7 ):
136
- $out .= $this->sesson[1];
137
- break;
138
- case( $mon < 10 ):
139
- $out .= $this->sesson[2];
140
- break;
141
- case( $mon > 9 ):
142
- $out .= $this->sesson[3];
143
- break;
144
- }
145
- break;
146
- case'F':
147
- $out .= $this->persian_month_names[ (int) $date['mon'] ];
148
- break;
149
- case'm':
150
- $out .= ( $date['mon'] < 10 ) ? '0' . $date['mon'] : $date['mon'];
151
- break;
152
- case'M':
153
- $out .= $this->persian_short_month_names[ (int) $date['mon'] ];
154
- break;
155
- case'n':
156
- $out .= $date['mon'];
157
- break;
158
- case'S':
159
- $out .= 'ام';
160
- break;
161
- case't':
162
- $out .= $this->j_days_in_month[ (int) $date['mon'] - 1 ];
163
- break;
164
- //year
165
- case'L':
166
- $out .= ( ( $date['year'] % 4 ) == 0 ) ? 1 : 0;
167
- break;
168
- case'o':
169
- case'Y':
170
- $out .= $date['year'];
171
- break;
172
- case'y':
173
- $out .= substr( $date['year'], 2, 2 );
174
- break;
175
- //time
176
- case'a':
177
- $out .= ( $date['hours'] < 12 ) ? 'ق.ظ' : 'ب.ظ';
178
- break;
179
- case'A':
180
- $out .= ( $date['hours'] < 12 ) ? 'قبل از ظهر' : 'بعد از ظهر';
181
- break;
182
- case'B':
183
- $out .= (int) ( 1 + ( $date['mon'] / 3 ) );
184
- break;
185
- case'g':
186
- $out .= ( $date['hours'] > 12 ) ? $date['hours'] - 12 : $date['hours'];
187
- break;
188
- case'G':
189
- $out .= $date['hours'];
190
- break;
191
- case'h':
192
- $hour = ( $date['hours'] > 12 ) ? $date['hours'] - 12 : $date['hours'];
193
- $out .= ( $hour < 10 ) ? '0' . $hour : $hour;
194
- break;
195
- case'H':
196
- $out .= ( $date['hours'] < 10 ) ? '0' . $date['hours'] : $date['hours'];
197
- break;
198
- case'i':
199
- $out .= ( $date['minutes'] < 10 ) ? '0' . $date['minutes'] : $date['minutes'];
200
- break;
201
- case's':
202
- $out .= ( $date['seconds'] < 10 ) ? '0' . $date['seconds'] : $date['seconds'];
203
- break;
204
- //full date time
205
- case'c':
206
- $out = $date['year'] . '/' . $date['mon'] . '/' . $date['mday'] . ' ' . $date['hours'] . ':' . ( ( $date['minutes'] < 10 ) ? '0' . $date['minutes'] : $date['minutes'] ) . ':' . ( ( $date['seconds'] < 10 ) ? '0' . $date['seconds'] : $date['seconds'] );//2004-02-12T15:19:21+00:00
207
- break;
208
- case'r':
209
- $out = $this->persian_day_names[ $date['wday'] ] . ',' . $date['mday'] . ' ' . $this->persian_month_names[ (int) $date['mon'] ] . ' ' . $date['year'] . ' ' . $date['hours'] . ':' . ( ( $date['minutes'] < 10 ) ? '0' . $date['minutes'] : $date['minutes'] ) . ':' . ( ( $date['seconds'] < 10 ) ? '0' . $date['seconds'] : $date['seconds'] );//Thu, 21 Dec 2000 16:01:07
210
- break;
211
- case'U':
212
- $out = $timestamp;
213
- break;
214
- //others
215
- case'e':
216
- case'I':
217
- case'i':
218
- case'O':
219
- case'P':
220
- case'T':
221
- case'Z':
222
- case'u':
223
- break;
224
- default:
225
- $out .= $format[ $i ];
226
- }
227
- }
228
- if ( $lang == 'per' ) {
229
- return self::trim_number( $out );
230
- } else {
231
- return $out;
232
- }
233
- }
234
 
235
- /**
236
- * bn_parsidate::gregorian_to_persian()
237
- * convert gregorian date to persian date
238
- *
239
- * @param mixed $gy
240
- * @param mixed $gm
241
- * @param mixed $gd
242
- *
243
- * @return array
244
- */
245
- function gregorian_to_persian( $gy, $gm, $gd ) {
246
- $dayofyear = $this->g_days_sum_month[ (int) $gm ] + $gd;
247
- if ( self::IsLeapYear( $gy ) and $gm > 2 ) {
248
- $dayofyear ++;
249
- }
250
- $d_33 = (int) ( ( ( $gy - 16 ) % 132 ) * 0.0305 );
251
- $leap = $gy % 4;
252
- $a = ( ( $d_33 == 1 or $d_33 == 2 ) and ( $d_33 == $leap or $leap == 1 ) ) ? 78 : ( ( $d_33 == 3 and $leap == 0 ) ? 80 : 79 );
253
- $b = ( $d_33 == 3 or $d_33 < ( $leap - 1 ) or $leap == 0 ) ? 286 : 287;
254
- if ( (int) ( ( $gy - 10 ) / 63 ) == 30 ) {
255
- $b --;
256
- $a ++;
257
- }
258
- if ( $dayofyear > $a ) {
259
- $jy = $gy - 621;
260
- $jd = $dayofyear - $a;
261
- } else {
262
- $jy = $gy - 622;
263
- $jd = $dayofyear + $b;
264
- }
265
- for ( $i = 0; $i < 11 and $jd > $this->j_days_in_month[ $i ]; $i ++ ) {
266
- $jd -= $this->j_days_in_month[ $i ];
267
- }
268
- $jm = ++ $i;
 
269
 
270
- return array( $jy, strlen( $jm ) == 1 ? '0' . $jm : $jm, strlen( $jd ) == 1 ? '0' . $jd : $jd );
271
- }
272
 
273
- /**
274
- * Get day of the week shamsi/jalali
275
- * @author Parsa Kafi
276
- *
277
- * @param int $wday
278
- *
279
- * @return int
280
- */
281
- private function week_day( $wday ) {
282
- if ( $wday == 6 ) {
283
- return 0;
284
- } else {
285
- return ++ $wday;
286
- }
287
- }
 
288
 
289
- /**
290
- * bn_parsidate::trim_number()
291
- * convert english number to persian number
292
- *
293
- * @param mixed $num
294
- * @param string $sp
295
- *
296
- * @return string
297
- */
298
- public function trim_number( $num, $sp = '٫' ) {
299
- $eng = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.' );
300
- $per = array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', $sp );
301
- $number = filter_var( $num, FILTER_SANITIZE_NUMBER_INT );
 
302
 
303
- return empty( $number ) ? str_replace( $per, $eng, $num ) : str_replace( $eng, $per, $num );
304
- }
305
 
306
- /**
307
- * bn_parsidate::getInstance()
308
- * create instance of bn_parsidate class
309
- *
310
- * @return instance
311
- */
312
- public static function getInstance() {
313
- if ( ! isset( self::$instance ) ) {
314
- self::$instance = new self();
315
- }
 
316
 
317
- return self::$instance;
318
- }
319
 
320
- /**
321
- * bn_parsidate::gregurian_date()
322
- * convert persian datetime to gregorian datetime
323
- *
324
- * @param mixed $format
325
- * @param mixed $persiandate
326
- *
327
- * @return mixed
328
- */
329
- public function gregurian_date( $format, $persiandate ) {
330
- preg_match_all( '!\d+!', $persiandate, $matches );
331
- $matches = $matches[0];
332
- list( $year, $mon, $day ) = self::persian_to_gregorian( $matches[0], $matches[1], $matches[2] );
 
333
 
334
- return date( $format, mktime( ( isset( $matches[3] ) ? $matches[3] : 0 ), ( isset( $matches[4] ) ? $matches[4] : 0 ), ( isset( $matches[5] ) ? $matches[5] : 0 ), $mon, $day, $year ) );
335
- }
336
 
337
- /**
338
- * bn_parsidate::persian_to_gregorian()
339
- * convert persian date to gregorian date
340
- *
341
- * @param mixed $jy
342
- * @param mixed $jm
343
- * @param mixed $jd
344
- *
345
- * @return array
346
- */
347
- public function persian_to_gregorian( $jy, $jm, $jd ) {
348
- $doyj = ( $jm - 2 > - 1 ? $this->j_days_sum_month[ (int) $jm ] + $jd : $jd );
349
- $d4 = ( $jy + 1 ) % 4;
350
- $d33 = (int) ( ( ( $jy - 55 ) % 132 ) * .0305 );
351
- $a = ( $d33 != 3 and $d4 <= $d33 ) ? 287 : 286;
352
- $b = ( ( $d33 == 1 or $d33 == 2 ) and ( $d33 == $d4 or $d4 == 1 ) ) ? 78 : ( ( $d33 == 3 and $d4 == 0 ) ? 80 : 79 );
353
- if ( (int) ( ( $jy - 19 ) / 63 ) == 20 ) {
354
- $a --;
355
- $b ++;
356
- }
357
- if ( $doyj <= $a ) {
358
- $gy = $jy + 621;
359
- $gd = $doyj + $b;
360
- } else {
361
- $gy = $jy + 622;
362
- $gd = $doyj - $a;
363
- }
364
- foreach ( array( 0, 31, ( $gy % 4 == 0 ) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ) as $gm => $days ) {
365
- if ( $gd <= $days ) {
366
- break;
367
- }
368
- $gd -= $days;
369
- }
 
 
370
 
371
- return array( $gy, $gm, $gd );
372
- }
373
  }
374
 
375
  /*
@@ -383,13 +409,14 @@ class bn_parsidate {
383
  * @param string $datetime
384
  * @param string $lang
385
  *
386
- * @return datetime
387
  */
388
- function parsidate( $input, $datetime = 'now', $lang = 'per' ) {
389
- $bndate = bn_parsidate::getInstance();
390
- $bndate = $bndate->persian_date( $input, $datetime, $lang );
 
391
 
392
- return $bndate;
393
  }
394
 
395
  /**
@@ -399,11 +426,12 @@ function parsidate( $input, $datetime = 'now', $lang = 'per' ) {
399
  * @param mixed $input
400
  * @param mixed $datetime
401
  *
402
- * @return datetime
403
  */
404
- function gregdate( $input, $datetime ) {
405
- $bndate = bn_parsidate::getInstance();
406
- $bndate = $bndate->gregurian_date( $input, $datetime );
 
407
 
408
- return $bndate;
409
  }
6
  * @package WP-Parsidate
7
  * @subpackage DateConversation
8
  */
9
+
10
  /*Special thanks to :
11
  Reza Gholampanahi for convert function*/
12
 
13
+ class bn_parsidate
14
+ {
15
+ protected static $instance;
16
+ public $persian_month_names = array(
17
+ '',
18
+ 'فروردین',
19
+ 'اردیبهشت',
20
+ 'خرداد',
21
+ 'تیر',
22
+ 'مرداد',
23
+ 'شهریور',
24
+ 'مهر',
25
+ 'آبان',
26
+ 'آذر',
27
+ 'دی',
28
+ 'بهمن',
29
+ 'اسفند'
30
+ );
31
+ public $persian_short_month_names = array(
32
+ '',
33
+ 'فرو',
34
+ 'ارد',
35
+ 'خرد',
36
+ 'تیر',
37
+ 'مرد',
38
+ 'شهر',
39
+ 'مهر',
40
+ 'آبا',
41
+ 'آذر',
42
+ 'دی',
43
+ 'بهم',
44
+ 'اسف'
45
+ );
46
+ public $sesson = array('بهار', 'تابستان', 'پاییز', 'زمستان');
47
+
48
+ public $persian_day_names = array('یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه', 'شنبه');
49
+ public $persian_day_small = array('ی', 'د', 'س', 'چ', 'پ', 'ج', 'ش');
50
+
51
+ public $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
52
+ private $j_days_sum_month = array(0, 0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336);
53
+
54
+ private $g_days_sum_month = array(0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
55
+
56
 
57
+ /**
58
+ * Constructor
59
+ */
60
+ function __construct()
61
+ {
62
+ if ($this->IsLeapYear($this->persian_date("Y", time(), "eng"))) {
63
+ $this->j_days_in_month[11] = 30;
64
+ }
65
+ }
66
 
67
+ /**
68
+ * bn_parsidate::IsLeapYear()
69
+ * check year is leap
70
+ * Update in 2.3.4V
71
+ *
72
+ * @param mixed $year
73
+ *
74
+ * @return boolean
75
+ */
76
+ public static function IsLeapYear($year)
77
+ {
78
+ // Update algorithm from https://goo.gl/wZCU76
79
+ $a = 0.025;
80
+ $b = 266;
81
+ if ($year > 0) {
82
+ $leapDays0 = (($year + 38) % 2820) * 0.24219 + $a; # 0.24219 ~ extra days of one year
83
+ $leapDays1 = (($year + 39) % 2820) * 0.24219 + $a; # 38 days is the difference of epoch to 2820-year cycle
84
+ } elseif ($year < 0) {
85
+ $leapDays0 = (($year + 39) % 2820) * 0.24219 + $a;
86
+ $leapDays1 = (($year + 40) % 2820) * 0.24219 + $a;
87
+ } else
88
+ return false;
89
 
90
+ $frac0 = intval(($leapDays0 - intval($leapDays0)) * 1000);
91
+ $frac1 = intval(($leapDays1 - intval($leapDays1)) * 1000);
92
 
93
+ return ($frac0 <= $b and $frac1 > $b);
94
+ }
95
 
96
+ /**
97
+ * bn_parsidate::persian_date()
98
+ * convert gregorian datetime to persian datetime
99
+ *
100
+ * @param mixed $format
101
+ * @param string $date
102
+ * @param string $lang
103
+ *
104
+ * @return datetime
105
+ */
106
+ public function persian_date($format, $date = 'now', $lang = 'per')
107
+ {
108
+ $j_days_in_month = array(31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365);
109
+ $timestamp = is_numeric($date) && (int)$date == $date ? $date : strtotime($date);
110
 
111
+ $date = getdate($timestamp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ list($date['year'], $date['mon'], $date['mday']) = self::gregorian_to_persian($date['year'], $date['mon'], $date['mday']);
114
+ $date['mon'] = (int)$date['mon'];
115
+ $date['mday'] = (int)$date['mday'];
116
+ $out = '';
117
+ $this->j_days_in_month[11] = $this->IsLeapYear($date['year']) ? 30 : 29;
 
 
 
 
 
 
 
 
118
 
119
+ for ($i = 0; $i < strlen($format); $i++) {
120
+ Switch ($format[$i]) {
121
+ //day
122
+ case 'd':
123
+ $out .= ($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday'];
124
+ break;
125
+ case 'D':
126
+ $out .= $this->persian_day_small[$date['wday']];
127
+ break;
128
+ case 'l':
129
+ $out .= $this->persian_day_names[$date['wday']];
130
+ break;
131
+ case 'j':
132
+ $out .= $date['mday'];
133
+ break;
134
+ case 'N':
135
+ $out .= $this->week_day($date['wday']) + 1;
136
+ break;
137
+ case 'w':
138
+ $out .= $this->week_day($date['wday']);
139
+ break;
140
+ case 'z':
141
+ $out .= $this->j_days_in_month[$date['mon']] + $date['mday'];
142
+ break;
143
+ //week
144
+ case 'W':
145
+ $yday = $this->j_days_sum_month[$date['mon'] - 1] + $date['mday'];
146
+ $out .= intval($yday / 7);
147
+ break;
148
+ //month
149
+ case 'f':
150
+ $mon = $date['mon'];
151
+ switch ($mon) {
152
+ case($mon < 4):
153
+ $out .= $this->sesson[0];
154
+ break;
155
+ case($mon < 7):
156
+ $out .= $this->sesson[1];
157
+ break;
158
+ case($mon < 10):
159
+ $out .= $this->sesson[2];
160
+ break;
161
+ case($mon > 9):
162
+ $out .= $this->sesson[3];
163
+ break;
164
+ }
165
+ break;
166
+ case'F':
167
+ $out .= $this->persian_month_names[(int)$date['mon']];
168
+ break;
169
+ case'm':
170
+ $out .= ($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon'];
171
+ break;
172
+ case'M':
173
+ $out .= $this->persian_short_month_names[(int)$date['mon']];
174
+ break;
175
+ case'n':
176
+ $out .= $date['mon'];
177
+ break;
178
+ case'S':
179
+ $out .= 'ام';
180
+ break;
181
+ case't':
182
+ $out .= $this->j_days_in_month[(int)$date['mon'] - 1];
183
+ break;
184
+ //year
185
+ case'L':
186
+ $out .= (($date['year'] % 4) == 0) ? 1 : 0;
187
+ break;
188
+ case'o':
189
+ case'Y':
190
+ $out .= $date['year'];
191
+ break;
192
+ case'y':
193
+ $out .= substr($date['year'], 2, 2);
194
+ break;
195
+ //time
196
+ case'a':
197
+ $out .= ($date['hours'] < 12) ? 'ق.ظ' : 'ب.ظ';
198
+ break;
199
+ case'A':
200
+ $out .= ($date['hours'] < 12) ? 'قبل از ظهر' : 'بعد از ظهر';
201
+ break;
202
+ case'B':
203
+ $out .= (int)(1 + ($date['mon'] / 3));
204
+ break;
205
+ case'g':
206
+ $out .= ($date['hours'] > 12) ? $date['hours'] - 12 : $date['hours'];
207
+ break;
208
+ case'G':
209
+ $out .= $date['hours'];
210
+ break;
211
+ case'h':
212
+ $hour = ($date['hours'] > 12) ? $date['hours'] - 12 : $date['hours'];
213
+ $out .= ($hour < 10) ? '0' . $hour : $hour;
214
+ break;
215
+ case'H':
216
+ $out .= ($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours'];
217
+ break;
218
+ case'i':
219
+ $out .= ($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes'];
220
+ break;
221
+ case's':
222
+ $out .= ($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'];
223
+ break;
224
+ //full date time
225
+ case'c':
226
+ $out = $date['year'] . '/' . $date['mon'] . '/' . $date['mday'] . ' ' . $date['hours'] . ':' . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':' . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);//2004-02-12T15:19:21+00:00
227
+ break;
228
+ case'r':
229
+ $out = $this->persian_day_names[$date['wday']] . ',' . $date['mday'] . ' ' . $this->persian_month_names[(int)$date['mon']] . ' ' . $date['year'] . ' ' . $date['hours'] . ':' . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':' . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);//Thu, 21 Dec 2000 16:01:07
230
+ break;
231
+ case'U':
232
+ $out = $timestamp;
233
+ break;
234
+ //others
235
+ case'e':
236
+ case'I':
237
+ case'O':
238
+ case'P':
239
+ case'T':
240
+ case'Z':
241
+ case'u':
242
+ break;
243
+ default:
244
+ $out .= $format[$i];
245
+ }
246
+ }
247
+ if ($lang == 'per') {
248
+ return self::trim_number($out);
249
+ } else {
250
+ return $out;
251
+ }
252
+ }
 
 
 
 
 
 
253
 
254
+ /**
255
+ * bn_parsidate::gregorian_to_persian()
256
+ * convert gregorian date to persian date
257
+ *
258
+ * @param mixed $gy
259
+ * @param mixed $gm
260
+ * @param mixed $gd
261
+ *
262
+ * @return array
263
+ */
264
+ function gregorian_to_persian($gy, $gm, $gd)
265
+ {
266
+ $dayofyear = $this->g_days_sum_month[(int)$gm] + $gd;
267
+ if (self::IsLeapYear($gy) and $gm > 2) {
268
+ $dayofyear++;
269
+ }
270
+ $d_33 = (int)((($gy - 16) % 132) * 0.0305);
271
+ $leap = $gy % 4;
272
+ $a = (($d_33 == 1 or $d_33 == 2) and ($d_33 == $leap or $leap == 1)) ? 78 : (($d_33 == 3 and $leap == 0) ? 80 : 79);
273
+ $b = ($d_33 == 3 or $d_33 < ($leap - 1) or $leap == 0) ? 286 : 287;
274
+ if ((int)(($gy - 10) / 63) == 30) {
275
+ $b--;
276
+ $a++;
277
+ }
278
+ if ($dayofyear > $a) {
279
+ $jy = $gy - 621;
280
+ $jd = $dayofyear - $a;
281
+ } else {
282
+ $jy = $gy - 622;
283
+ $jd = $dayofyear + $b;
284
+ }
285
+ for ($i = 0; $i < 11 and $jd > $this->j_days_in_month[$i]; $i++) {
286
+ $jd -= $this->j_days_in_month[$i];
287
+ }
288
+ $jm = ++$i;
289
 
290
+ return array($jy, strlen($jm) == 1 ? '0' . $jm : $jm, strlen($jd) == 1 ? '0' . $jd : $jd);
291
+ }
292
 
293
+ /**
294
+ * Get day of the week shamsi/jalali
295
+ * @author Parsa Kafi
296
+ *
297
+ * @param int $wday
298
+ *
299
+ * @return int
300
+ */
301
+ private function week_day($wday)
302
+ {
303
+ if ($wday == 6) {
304
+ return 0;
305
+ } else {
306
+ return ++$wday;
307
+ }
308
+ }
309
 
310
+ /**
311
+ * bn_parsidate::trim_number()
312
+ * convert english number to persian number
313
+ *
314
+ * @param mixed $num
315
+ * @param string $sp
316
+ *
317
+ * @return string
318
+ */
319
+ public function trim_number($num, $sp = '٫')
320
+ {
321
+ $eng = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.');
322
+ $per = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', $sp);
323
+ $number = filter_var($num, FILTER_SANITIZE_NUMBER_INT);
324
 
325
+ return empty($number) ? str_replace($per, $eng, $num) : str_replace($eng, $per, $num);
326
+ }
327
 
328
+ /**
329
+ * bn_parsidate::getInstance()
330
+ * create instance of bn_parsidate class
331
+ *
332
+ * @return instance
333
+ */
334
+ public static function getInstance()
335
+ {
336
+ if (!isset(self::$instance)) {
337
+ self::$instance = new self();
338
+ }
339
 
340
+ return self::$instance;
341
+ }
342
 
343
+ /**
344
+ * bn_parsidate::gregurian_date()
345
+ * convert persian datetime to gregorian datetime
346
+ *
347
+ * @param mixed $format
348
+ * @param mixed $persiandate
349
+ *
350
+ * @return mixed
351
+ */
352
+ public function gregurian_date($format, $persiandate)
353
+ {
354
+ preg_match_all('!\d+!', $persiandate, $matches);
355
+ $matches = $matches[0];
356
+ list($year, $mon, $day) = self::persian_to_gregorian($matches[0], $matches[1], $matches[2]);
357
 
358
+ return date($format, mktime((isset($matches[3]) ? $matches[3] : 0), (isset($matches[4]) ? $matches[4] : 0), (isset($matches[5]) ? $matches[5] : 0), $mon, $day, $year));
359
+ }
360
 
361
+ /**
362
+ * bn_parsidate::persian_to_gregorian()
363
+ * convert persian date to gregorian date
364
+ *
365
+ * @param mixed $jy
366
+ * @param mixed $jm
367
+ * @param mixed $jd
368
+ *
369
+ * @return array
370
+ */
371
+ public function persian_to_gregorian($jy, $jm, $jd)
372
+ {
373
+ $this->j_days_in_month[11] = $this->IsLeapYear($jy) ? 30 : 29;
374
+ $doyj = ($jm - 2 > -1 ? $this->j_days_sum_month[(int)$jm] + $jd : $jd);
375
+ $d4 = ($jy + 1) % 4;
376
+ $d33 = (int)((($jy - 55) % 132) * .0305);
377
+ $a = ($d33 != 3 and $d4 <= $d33) ? 287 : 286;
378
+ $b = (($d33 == 1 or $d33 == 2) and ($d33 == $d4 or $d4 == 1)) ? 78 : (($d33 == 3 and $d4 == 0) ? 80 : 79);
379
+ if ((int)(($jy - 19) / 63) == 20) {
380
+ $a--;
381
+ $b++;
382
+ }
383
+ if ($doyj <= $a) {
384
+ $gy = $jy + 621;
385
+ $gd = $doyj + $b;
386
+ } else {
387
+ $gy = $jy + 622;
388
+ $gd = $doyj - $a;
389
+ }
390
+ foreach (array(0, 31, ($gy % 4 == 0) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) as $gm => $days) {
391
+ if ($gd <= $days) {
392
+ break;
393
+ }
394
+ $gd -= $days;
395
+ }
396
 
397
+ return array($gy, $gm, $gd);
398
+ }
399
  }
400
 
401
  /*
409
  * @param string $datetime
410
  * @param string $lang
411
  *
412
+ * @return string
413
  */
414
+ function parsidate($input, $datetime = 'now', $lang = 'per')
415
+ {
416
+ $bndate = bn_parsidate::getInstance();
417
+ $bndate = $bndate->persian_date($input, $datetime, $lang);
418
 
419
+ return $bndate;
420
  }
421
 
422
  /**
426
  * @param mixed $input
427
  * @param mixed $datetime
428
  *
429
+ * @return string
430
  */
431
+ function gregdate($input, $datetime)
432
+ {
433
+ $bndate = bn_parsidate::getInstance();
434
+ $bndate = $bndate->gregurian_date($input, $datetime);
435
 
436
+ return $bndate;
437
  }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://forum.wp-parsi.com/
4
  Tags: shamsi, wp-parsi, wpparsi, persian, parsi, farsi, jalali, date, calendar, i18n, l10n, Iran, Iranian, parsidate, rtl
5
  Requires at least: 3.6
6
  Tested up to: 4.8
7
- Stable tag: 2.3.3
8
 
9
  Persian date support for WordPress
10
 
@@ -41,6 +41,9 @@ List of some features:
41
  4. 'Jalali Date Calender' in action
42
 
43
  == Changelog ==
 
 
 
44
  = 2.3.3 =
45
  * Fix WooCommerce Sale Price Dates (From/To)
46
 
4
  Tags: shamsi, wp-parsi, wpparsi, persian, parsi, farsi, jalali, date, calendar, i18n, l10n, Iran, Iranian, parsidate, rtl
5
  Requires at least: 3.6
6
  Tested up to: 4.8
7
+ Stable tag: 2.3.4
8
 
9
  Persian date support for WordPress
10
 
41
  4. 'Jalali Date Calender' in action
42
 
43
  == Changelog ==
44
+ = 2.3.4 =
45
+ * Fix diagnosis leap year function ( bn_parsidate::IsLeapYear() )
46
+
47
  = 2.3.3 =
48
  * Fix WooCommerce Sale Price Dates (From/To)
49
 
wp-parsidate.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /**
3
  * Plugin Name: WP-Parsidate
4
- * Version: 2.3.3
5
  * Plugin URI: http://forum.wp-parsi.com/
6
  * Description: Persian package for WordPress, Adds full RTL and Shamsi (Jalali) support for: posts, comments, pages, archives, search, categories, permalinks and all admin sections and TinyMce editor, lists, quick editor. This package has Jalali archive widget.
7
  * Author: WP-Parsi Team
1
  <?php
2
  /**
3
  * Plugin Name: WP-Parsidate
4
+ * Version: 2.3.4
5
  * Plugin URI: http://forum.wp-parsi.com/
6
  * Description: Persian package for WordPress, Adds full RTL and Shamsi (Jalali) support for: posts, comments, pages, archives, search, categories, permalinks and all admin sections and TinyMce editor, lists, quick editor. This package has Jalali archive widget.
7
  * Author: WP-Parsi Team