I18N_Arabic

# Source for file Mktime.php

Documentation is available at Mktime.php

1. <?php
2. /**
3. * ----------------------------------------------------------------------
4. *
5. * Copyright (c) 2006-2016 Khaled Al-Sham'aa.
6. *
7. * http://www.ar-php.org
8. *
9. * PHP Version 5
10. *
11. * ----------------------------------------------------------------------
12. *
13. * LICENSE
14. *
15. * This program is open source product; you can redistribute it and/or
16. * modify it under the terms of the GNU Lesser General Public License (LGPL)
17. * as published by the Free Software Foundation; either version 3
18. * of the License, or (at your option) any later version.
19. *
20. * This program is distributed in the hope that it will be useful,
21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23. * GNU Lesser General Public License for more details.
24. *
25. * You should have received a copy of the GNU Lesser General Public License
26. * along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
27. *
28. * ----------------------------------------------------------------------
29. *
30. * Class Name: Arabic Maketime
31. *
32. * Filename: Mktime.php
33. *
34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
35. *
36. * Purpose: Arabic customization for PHP mktime function
37. *
38. * ----------------------------------------------------------------------
39. *
40. * Arabic Maketime
41. *
42. * PHP class for Arabic and Islamic customization of PHP mktime function.
43. * It can convert Hijri date into UNIX timestamp format
44. *
45. * Unix time() value:
46. *
47. * Development of the Unix operating system began at Bell Laboratories in 1969 by
48. * Dennis Ritchie and Ken Thompson, with the first PDP-11 version becoming
49. * operational in February 1971. Unix wisely adopted the convention that all
50. * internal dates and times (for example, the time of creation and last modification
51. * of files) were kept in Universal Time, and converted to local time based on a
52. * per-user time zone specification. This far-sighted choice has made it vastly
53. * easier to integrate Unix systems into far-flung networks without a chaos of
54. * conflicting time settings.
55. *
56. * The machines on which Unix was developed and initially deployed could not support
57. * arithmetic on integers longer than 32 bits without costly multiple-precision
58. * computation in software. The internal representation of time was therefore chosen
59. * to be the number of seconds elapsed since 00:00 Universal time on January 1, 1970
60. * in the Gregorian calendar (Julian day 2440587.5), with time stored as a 32 bit
61. * signed integer (long in the original C implementation).
62. *
63. * The influence of Unix time representation has spread well beyond Unix since most
64. * C and C++ libraries on other systems provide Unix-compatible time and date
65. * functions. The major drawback of Unix time representation is that, if kept as a
66. * 32 bit signed quantity, on January 19, 2038 it will go negative, resulting in
67. * chaos in programs unprepared for this. Modern Unix and C implementations define
68. * the result of the time() function as type time_t, which leaves the door open for
69. * remediation (by changing the definition to a 64 bit integer, for example) before
70. * the clock ticks the dreaded doomsday second.
71. *
72. * mktime -- Get Unix timestamp for a date
73. * int mktime (int hour, int minute, int second, int month, int day, int year);
74. *
75. * Warning: Note the strange order of arguments, which differs from the order of
76. * arguments in a regular Unix mktime() call and which does not lend itself well to
77. * leaving out parameters from right to left (see below). It is a common error to
78. * mix these values up in a script.
79. *
80. * Returns the Unix timestamp corresponding to the arguments given. This timestamp
81. * is a long integer containing the number of seconds between the Unix Epoch
82. * (January 1 1970) and the time specified.
83. *
84. * Example:
85. * <code>
86. * date_default_timezone_set('UTC');
87. *
88. * include('./I18N/Arabic.php');
89. * $obj = new I18N_Arabic('Mktime');
90. *
91. * $time = $obj->mktime(0,0,0,9,1,1427);
92. *
93. * echo "<p>Calculated first day of Ramadan 1427 unix timestamp is: $time</p>";
94. *
95. * $Gregorian = date('l F j, Y',$time);
96. *
97. * echo "<p>Which is $Gregorian in Gregorian calendar</p>";
98. * </code>
99. *
100. * @category I18N
101. * @package I18N_Arabic
102. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
103. * @copyright 2006-2016 Khaled Al-Sham'aa
104. *
105. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
106. * @link http://www.ar-php.org
107. */
109. /**
110. * This PHP class is an Arabic customization for PHP mktime function
111. *
112. * @category I18N
113. * @package I18N_Arabic
114. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
115. * @copyright 2006-2016 Khaled Al-Sham'aa
116. *
117. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
118. * @link http://www.ar-php.org
119. */
120. class I18N_Arabic_Mktime
121. {
122. /**
123. * Loads initialize values
124. *
125. * @ignore
126. */
127. public function __construct()
128. {
129. }
131. /**
132. * This will return current Unix timestamp
133. * for given Hijri date (Islamic calendar)
134. *
135. * @param integer $hour Time hour
136. * @param integer $minute Time minute
137. * @param integer $second Time second
138. * @param integer $hj_month Hijri month (Islamic calendar)
139. * @param integer $hj_day Hijri day (Islamic calendar)
140. * @param integer $hj_year Hijri year (Islamic calendar)
141. * @param integer $correction To apply correction factor (+/- 1-2)
142. * to standard Hijri calendar
143. *
144. * @return integer Returns the current time measured in the number of
145. * seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
146. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
147. */
148. public function mktime(
149. $hour, $minute, $second, $hj_month, $hj_day, $hj_year, $correction = 0
150. ) {
151. list($year, $month, $day) = $this->convertDate($hj_year, $hj_month, $hj_day);
153. $unixTimeStamp = mktime($hour, $minute, $second, $month, $day, $year);
155. $unixTimeStamp = $unixTimeStamp + 3600*24*$correction;
157. return $unixTimeStamp;
158. }
160. /**
161. * This will convert given Hijri date (Islamic calendar) into Gregorian date
162. *
163. * @param integer $Y Hijri year (Islamic calendar)
164. * @param integer $M Hijri month (Islamic calendar)
165. * @param integer $D Hijri day (Islamic calendar)
166. *
167. * @return array Gregorian date [int Year, int Month, int Day]
168. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
169. */
170. protected function convertDate($Y, $M, $D)
171. {
172. if (function_exists('GregorianToJD')) {
173. $str = JDToGregorian($this->islamicToJd($Y, $M, $D));
174. } else {
175. $str = $this->jdToGreg($this->islamicToJd($Y, $M, $D));
176. }
178. list($month, $day, $year) = explode('/', $str);
180. return array($year, $month, $day);
181. }
183. /**
184. * This will convert given Hijri date (Islamic calendar) into Julian day
185. *
186. * @param integer $year Hijri year
187. * @param integer $month Hijri month
188. * @param integer $day Hijri day
189. *
190. * @return integer Julian day
191. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
192. */
193. protected function islamicToJd($year, $month, $day)
194. {
195. $jd = (int)((11 * $year + 3) / 30) + (int)(354 * $year) + (int)(30 * $month)
196. - (int)(($month - 1) / 2) + $day + 1948440 - 385;
197. return $jd;
198. }
200. /**
201. * Converts Julian Day Count to Gregorian date
202. *
203. * @param integer $julian A julian day number as integer
204. *
205. * @return integer The gregorian date as a string in the form "month/day/year"
206. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
207. */
208. protected function jdToGreg($julian)
209. {
210. $julian = $julian - 1721119;
211. $calc1 = 4 * $julian - 1;
212. $year = floor($calc1 / 146097);
213. $julian = floor($calc1 - 146097 * $year);
214. $day = floor($julian / 4);
215. $calc2 = 4 * $day + 3;
216. $julian = floor($calc2 / 1461);
217. $day = $calc2 - 1461 * $julian;
218. $day = floor(($day + 4) / 4);
219. $calc3 = 5 * $day - 3;
220. $month = floor($calc3 / 153);
221. $day = $calc3 - 153 * $month;
222. $day = floor(($day + 5) / 5);
223. $year = 100 * $year + $julian;
225. if ($month < 10) {
226. $month = $month + 3;
227. } else {
228. $month = $month - 9;
229. $year = $year + 1;
230. }
232. /*
233. Just to mimic the PHP JDToGregorian output
234. If year is less than 1, subtract one to convert from
235. a zero based date system to the common era system in
236. which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.)
237. */
239. if ($year < 1) {
240. $year--;
241. }
243. return $month.'/'.$day.'/'.$year;
244. }
246. /**
247. * Calculate Hijri calendar correction using Um-Al-Qura calendar information
248. *
249. * @param integer $m Hijri month (Islamic calendar)
250. * @param integer $y Hijri year (Islamic calendar), valid range [1420-1459]
251. *
252. * @return integer Correction factor to fix Hijri calendar calculation using
253. * Um-Al-Qura calendar information
254. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
255. */
256. public function mktimeCorrection ($m, $y)
257. {
258. if ($y >= 1420 && $y < 1460) {
259. $calc = $this->mktime(0, 0, 0, $m, 1, $y);
260. $file = dirname(__FILE__).'/data/um_alqoura.txt';
262. $content = file_get_contents($file);
263. $offset = (($y-1420) * 12 + $m) * 11;
265. $d = substr($content, $offset, 2);
266. $m = substr($content, $offset+3, 2);
267. $y = substr($content, $offset+6, 4);
269. $real = mktime(0, 0, 0, $m, $d, $y);
271. $diff = (int)(($real - $calc) / (3600 * 24));
272. } else {
273. $diff = 0;
274. }
276. return $diff;
277. }
279. /**
280. * Calculate how many days in a given Hijri month
281. *
282. * @param integer $m Hijri month (Islamic calendar)
283. * @param integer $y Hijri year (Islamic calendar), valid
284. * range[1320-1459]
285. * @param boolean $umAlqoura Should we implement Um-Al-Qura calendar correction
286. * in this calculation (default value is true)
287. *
288. * @return integer Days in a given Hijri month
289. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
290. */
291. public function hijriMonthDays ($m, $y, $umAlqoura = true)
292. {
293. if ($y >= 1320 && $y < 1460) {
294. $begin = $this->mktime(0, 0, 0, $m, 1, $y);
296. if ($m == 12) {
297. $m2 = 1;
298. $y2 = $y + 1;
299. } else {
300. $m2 = $m + 1;
301. $y2 = $y;
302. }
304. $end = $this->mktime(0, 0, 0, $m2, 1, $y2);
306. if ($umAlqoura === true) {
307. $c1 = $this->mktimeCorrection($m, $y);
308. $c2 = $this->mktimeCorrection($m2, $y2);
309. } else {
310. $c1 = 0;
311. $c2 = 0;
312. }
314. $days = ($end - $begin) / (3600 * 24);
315. $days = $days - $c1 + $c2;
316. } else {
317. $days = false;
318. }
320. return $days;
321. }
322. }

Documentation generated on Fri, 01 Jan 2016 10:26:08 +0200 by phpDocumentor 1.4.0