-
Notifications
You must be signed in to change notification settings - Fork 0
/
jalaali.php
267 lines (239 loc) · 7.77 KB
/
jalaali.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace Jalaali;
/**
* Jalaali PHP Implementation based on [JalaaliJS](https://github.com/jalaali/jalaali-js).
*
* @author Navid Emami <[email protected]>
* @license MIT
* @license https://opensource.org/licenses/MIT
* @version 1.0.0 Initial Release
*/
class Jalaali {
/**
* Converts a Gregorian date to Jalaali.
*
* @param integer $gy Gregorian Year
* @param integer $gm Gregorian Month
* @param integer $gd Gregorian Day
*
* @return array The converted Jalaali date
*/
public static function toJalaali($gy, $gm, $gd) {
return self::d2j(self::g2d($gy, $gm, $gd));
}
/**
* Converts a Jalaali date to Gregorian.
*
* @param integer $jy Jalaali Year
* @param integer $jm Jalaali Month
* @param integer $jd Jalaali Day
*
* @return array The converted Gregorian date
*/
public static function toGregorian($jy, $jm, $jd) {
return self::d2g(self::j2d($jy, $jm, $jd));
}
/**
* Checks whether a Jalaali date is valid or not.
*
* @param integer $jy Jalaali Year.
* @param integer $jm Jalaali Month.
* @param integer $jd Jalaali Day.
*
* @return boolean is date valid?
*/
public static function isValidJalaaliDate($jy, $jm, $jd) {
$yearIsValid = ($jy >= -61 && $jy <= 3177);
$monthIsValid = ($jm >= 1 && $jm <= 12);
$dayIsValid = ($jd >= 1 && $jd <= self::jalaaliMonthLength($jy, $jm));
return $yearIsValid && $monthIsValid && $dayIsValid;
}
/**
* Checks whether this is a leap year or not.
*
* @param integer $jy Jalaali Year.
*
* @return boolean is leap year?
*/
public static function isLeapJalaaliYear($jy) {
$result = self::jalaaliCalendar($jy);
return $result['leap'] == 0;
}
/**
* Number of days in a given month in Jalaali year.
*
* @param integer $jy Jalaali Year.
* @param integer $jm Jalaali Month.
*
* @return integer
*/
public static function jalaaliMonthLength($jy, $jm) {
if ($jm <= 6) return 31;
if ($jm <= 11) return 30;
if (self::isLeapJalaaliYear($jy)) return 30;
return 29;
}
/**
* This function determines if the Jalaali (Persian) year is
* leap (366-day long) or is the common year (365 days), and
* finds the day in March (Gregorian calendar) of the first
* day of the Jalaali year (jy).
*
* @param integer $jy Jalaali Year (-61 to 3177).
*
* @return array Containing leap, gy and march.
*
* @see http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm
* @see http://www.fourmilab.ch/documents/calendar
*/
public static function jalaaliCalendar($jy) {
// jalaali years starting the 33 - year rule.
$breaks = array(-61 ,9 ,38 ,199 ,426 ,686 ,756 ,818 ,1111 ,1181 ,1210 ,1635 ,2060 ,2097 ,2192 ,2262 ,2324 ,2394 ,2456 ,3178);
$bl = count($breaks);
$gy = $jy + 621;
$leapJ = -14;
$jp = $breaks[0];
if ($jy < $jp || $jy >= $breaks[$bl - 1]) {
throw new \Exception('Invalid Jalaali Year: ' . $jy);
}
// find the limiting years for the jalaali year jy.
for ($i = 1; $i < $bl; $i++) {
$jm = $breaks[$i];
$jump = $jm - $jp;
if ($jy < $jm) {
break;
}
$leapJ = $leapJ + self::div($jump, 33) * 8 + self::div(self::mod($jump, 33), 4);
$jp = $jm;
}
$n = $jy - $jp;
// find the number of leap years from AD 621 to the beginning of the current jalaali year in the persian calendar.
$leapJ = $leapJ + self::div($n, 33) * 8 + self::div(self::mod($n, 33) + 3, 4);
if (self::mod($jump, 33) == 4 && ($jump - $n) == 4) {
$leapJ += 1;
}
// and the same in the gregorian calendar (until the year gy).
$leapG = self::div($gy, 4) - self::div((self::div($gy, 100) + 1) * 3, 4) - 150;
// determine the gregorian date of Farvardin the 1st.
$march = 20 + $leapJ - $leapG;
// find how many years have passed since the last leap year.
if ($jump - $n < 6) {
$n = $n - $jump + self::div($jump + 4, 33) * 33;
}
$leap = self::mod(self::mod($n + 1, 33) - 1, 4);
if ($leap == -1) {
$leap = 4;
}
$result = array('leap' => $leap, 'gy' => $gy, 'march' => $march);
return $result;
}
/**
* Converts a date of the Jalaali calendar to the Julian Day Number.
*
* @param integer $jy Jalaali Year (1 to 3100)
* @param integer $jm Jalaali Month (1 to 12)
* @param integer $jd Jalaali Day (1 to 29/31)
*
* @return $jdn Julian Day Number
*/
public static function j2d($jy, $jm, $jd) {
$result = self::jalaaliCalendar($jy);
return self::g2d($result['gy'], 3, $result['march']) + ($jm - 1) * 31 - self::div($jm, 7) * ($jm - 7) + $jd - 1;
}
/**
* Converts the Julian Day Number to a date in the Jalaali calendar.
*
* @param integer $jdn Julian Day Number
*
* @return array Containing $jy, $jm and $jd
*/
public static function d2j($jdn) {
// calculate gregorian year
$result = self::d2g($jdn);
$gy = $result['gy'];
$jy = $gy - 621;
$result = self::jalaaliCalendar($jy);
$jdn1f = self::g2d($gy, 3, $result['march']);
// find number of days that passed since 1 farvardin.
$k = $jdn - $jdn1f;
if ($k >= 0) {
if ($k <= 185) {
// The first 6 months.
$jm = self::div($k, 31) + 1;
$jd = self::mod($k, 31) + 1;
$date = array('jy' => $jy, 'jm' => $jm, 'jd' => $jd);
return $date;
} else {
// The remaining months.
$k -= 186;
}
} else {
// previous jalaali year.
$jy -= 1;
$k += 179;
if ($result['leap'] == 1) $k += 1;
}
$jm = self::div($k, 30) + 7;
$jd = self::mod($k, 30) + 1;
$date = array('jy' => $jy, 'jm' => $jm, 'jd' => $jd);
return $date;
}
/**
* Calculates the Julian Day number from Gregorian or Julian
* calendar dates. This integer number corresponds to the noon of
* the date (i.e. 12 hours of Universal Time).
* The procedure was tested to be good since 1 March, -100100 (of both
* calendars) up to a few million years into the future.
*
* @param integer $gy Gregorian Year
* @param integer $gm Gregorian Month (1 to 12)
* @param integer $gd Gregorian Day (1 to 28/29/30/31)
*
* @return $jdn Julian Day Number
*/
public static function g2d($gy, $gm, $gd) {
$jdn = self::div(($gy + self::div($gm - 8, 6) + 100100) * 1461, 4) + self::div(153 * self::mod($gm + 9, 12) + 2, 5) + $gd - 34840408;
$jdn = $jdn - self::div(self::div($gy + 100100 + self::div($gm - 8, 6), 100) * 3, 4) + 752;
return $jdn;
}
/**
* Calculates Gregorian and Julian calendar dates from the Julian Day number
* (jdn) for the period since jdn=-34839655 (i.e. the year -100100 of both
* calendars) to some millions years ahead of the present.
*
* @param integer $jdn Julian Day Number
*
* @return array Containing $gy, $gm and $gd
*/
public static function d2g($jdn) {
$j = 4 * $jdn + 139361631 + self::div(self::div(4 * $jdn + 183187720, 146097) * 3, 4) * 4 - 3908;
$i = self::div(self::mod($j, 1461), 4) * 5 + 308;
$gd = self::div(self::mod($i, 153), 5) + 1;
$gm = self::mod(self::div($i, 153), 12) + 1;
$gy = self::div($j, 1461) - 100100 + self::div(8 - $gm, 6);
$date = array('gy' => $gy, 'gm' => $gm, 'gd' => $gd);
return $date;
}
/**
* Helper function for division.
*
* @param integer $first First Parameter
* @param integer $second Second Parameter
*
* @return integer division result
*/
private function div($first, $second) {
return ~~($first / $second);
}
/**
* Helper function for self::modulo.
*
* @param integer $first First Parameter
* @param integer $second Second Parameter
*
* @return integer self::modulo result
*/
private function mod($first, $second) {
return $first - ~~($first / $second) * $second;
}
}