PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Arabic/Mktime.php

https://bitbucket.org/swergroup/wp-uploadplus
PHP | 333 lines | 101 code | 27 blank | 205 comment | 16 complexity | 38ecec96d665c028015aa5dbf8ce8a2b MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------
  4. *
  5. * Copyright (c) 2006-2013 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-2013 Khaled Al-Sham'aa
  104. *
  105. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
  106. * @link http://www.ar-php.org
  107. */
  108. // New in PHP V5.3: Namespaces
  109. // namespace I18N\Arabic;
  110. //
  111. // $obj = new I18N\Arabic\Mktime();
  112. //
  113. // use I18N\Arabic;
  114. // $obj = new Arabic\Mktime();
  115. //
  116. // use I18N\Arabic\Mktime as Mktime;
  117. // $obj = new Mktime();
  118. /**
  119. * This PHP class is an Arabic customization for PHP mktime function
  120. *
  121. * @category I18N
  122. * @package I18N_Arabic
  123. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  124. * @copyright 2006-2013 Khaled Al-Sham'aa
  125. *
  126. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
  127. * @link http://www.ar-php.org
  128. */
  129. class I18N_Arabic_Mktime
  130. {
  131. /**
  132. * Loads initialize values
  133. *
  134. * @ignore
  135. */
  136. public function __construct()
  137. {
  138. }
  139. /**
  140. * This will return current Unix timestamp
  141. * for given Hijri date (Islamic calendar)
  142. *
  143. * @param integer $hour Time hour
  144. * @param integer $minute Time minute
  145. * @param integer $second Time second
  146. * @param integer $hj_month Hijri month (Islamic calendar)
  147. * @param integer $hj_day Hijri day (Islamic calendar)
  148. * @param integer $hj_year Hijri year (Islamic calendar)
  149. * @param integer $correction To apply correction factor (+/- 1-2)
  150. * to standard Hijri calendar
  151. *
  152. * @return integer Returns the current time measured in the number of
  153. * seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  154. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  155. */
  156. public function mktime(
  157. $hour, $minute, $second, $hj_month, $hj_day, $hj_year, $correction = 0
  158. ) {
  159. list($year, $month, $day) = $this->convertDate($hj_year, $hj_month, $hj_day);
  160. $unixTimeStamp = mktime($hour, $minute, $second, $month, $day, $year);
  161. $unixTimeStamp = $unixTimeStamp + 3600*24*$correction;
  162. return $unixTimeStamp;
  163. }
  164. /**
  165. * This will convert given Hijri date (Islamic calendar) into Gregorian date
  166. *
  167. * @param integer $Y Hijri year (Islamic calendar)
  168. * @param integer $M Hijri month (Islamic calendar)
  169. * @param integer $D Hijri day (Islamic calendar)
  170. *
  171. * @return array Gregorian date [int Year, int Month, int Day]
  172. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  173. */
  174. protected function convertDate($Y, $M, $D)
  175. {
  176. if (function_exists('GregorianToJD')) {
  177. $str = JDToGregorian($this->islamicToJd($Y, $M, $D));
  178. } else {
  179. $str = $this->jdToGreg($this->islamicToJd($Y, $M, $D));
  180. }
  181. list($month, $day, $year) = explode('/', $str);
  182. return array($year, $month, $day);
  183. }
  184. /**
  185. * This will convert given Hijri date (Islamic calendar) into Julian day
  186. *
  187. * @param integer $year Hijri year
  188. * @param integer $month Hijri month
  189. * @param integer $day Hijri day
  190. *
  191. * @return integer Julian day
  192. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  193. */
  194. protected function islamicToJd($year, $month, $day)
  195. {
  196. $jd = (int)((11 * $year + 3) / 30) + (int)(354 * $year) + (int)(30 * $month)
  197. - (int)(($month - 1) / 2) + $day + 1948440 - 385;
  198. return $jd;
  199. }
  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;
  224. if ($month < 10) {
  225. $month = $month + 3;
  226. } else {
  227. $month = $month - 9;
  228. $year = $year + 1;
  229. }
  230. /*
  231. Just to mimic the PHP JDToGregorian output
  232. If year is less than 1, subtract one to convert from
  233. a zero based date system to the common era system in
  234. which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.)
  235. */
  236. if ($year < 1) {
  237. $year--;
  238. }
  239. return $month.'/'.$day.'/'.$year;
  240. }
  241. /**
  242. * Calculate Hijri calendar correction using Um-Al-Qura calendar information
  243. *
  244. * @param integer $m Hijri month (Islamic calendar)
  245. * @param integer $y Hijri year (Islamic calendar), valid range [1420-1459]
  246. *
  247. * @return integer Correction factor to fix Hijri calendar calculation using
  248. * Um-Al-Qura calendar information
  249. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  250. */
  251. public function mktimeCorrection ($m, $y)
  252. {
  253. if ($y >= 1420 && $y < 1460) {
  254. $calc = $this->mktime(0, 0, 0, $m, 1, $y);
  255. $file = dirname(__FILE__).'/data/um_alqoura.txt';
  256. $content = file_get_contents($file);
  257. $offset = (($y-1420) * 12 + $m) * 11;
  258. $d = substr($content, $offset, 2);
  259. $m = substr($content, $offset+3, 2);
  260. $y = substr($content, $offset+6, 4);
  261. $real = mktime(0, 0, 0, $m, $d, $y);
  262. $diff = (int)(($real - $calc) / (3600 * 24));
  263. } else {
  264. $diff = 0;
  265. }
  266. return $diff;
  267. }
  268. /**
  269. * Calculate how many days in a given Hijri month
  270. *
  271. * @param integer $m Hijri month (Islamic calendar)
  272. * @param integer $y Hijri year (Islamic calendar), valid
  273. * range[1320-1459]
  274. * @param boolean $umAlqoura Should we implement Um-Al-Qura calendar correction
  275. * in this calculation (default value is true)
  276. *
  277. * @return integer Days in a given Hijri month
  278. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  279. */
  280. public function hijriMonthDays ($m, $y, $umAlqoura = true)
  281. {
  282. if ($y >= 1320 && $y < 1460) {
  283. $begin = $this->mktime(0, 0, 0, $m, 1, $y);
  284. if ($m == 12) {
  285. $m2 = 1;
  286. $y2 = $y + 1;
  287. } else {
  288. $m2 = $m + 1;
  289. $y2 = $y;
  290. }
  291. $end = $this->mktime(0, 0, 0, $m2, 1, $y2);
  292. if ($umAlqoura === true) {
  293. $c1 = $this->mktimeCorrection($m, $y);
  294. $c2 = $this->mktimeCorrection($m2, $y2);
  295. } else {
  296. $c1 = 0;
  297. $c2 = 0;
  298. }
  299. $days = ($end - $begin) / (3600 * 24);
  300. $days = $days - $c1 + $c2;
  301. } else {
  302. $days = false;
  303. }
  304. return $days;
  305. }
  306. }