PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/third_party/ar-php/Arabic/Mktime.php

https://bitbucket.org/sammousa/valuematchbv-ls2
PHP | 282 lines | 65 code | 24 blank | 193 comment | 2 complexity | 010dd090f6bbd82522d9f68f249bdfe7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------
  4. *
  5. * Copyright (c) 2006-2012 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-2012 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-2012 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. private static $_islamicEpoch = 1948439.5;
  132. /**
  133. * Loads initialize values
  134. *
  135. * @ignore
  136. */
  137. public function __construct()
  138. {
  139. }
  140. /**
  141. * This will return current Unix timestamp
  142. * for given Hijri date (Islamic calendar)
  143. *
  144. * @param integer $hour Time hour
  145. * @param integer $minute Time minute
  146. * @param integer $second Time second
  147. * @param integer $hj_month Hijri month (Islamic calendar)
  148. * @param integer $hj_day Hijri day (Islamic calendar)
  149. * @param integer $hj_year Hijri year (Islamic calendar)
  150. * @param integer $correction To apply correction factor (+/- 1-2)
  151. * to standard Hijri calendar
  152. *
  153. * @return integer Returns the current time measured in the number of
  154. * seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  155. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  156. */
  157. public function mktime($hour, $minute, $second,
  158. $hj_month, $hj_day, $hj_year, $correction = 0)
  159. {
  160. list($year, $month, $day) = $this->convertDate($hj_year, $hj_month, $hj_day);
  161. $unixTimeStamp = mktime($hour, $minute, $second, $month, $day, $year);
  162. $unixTimeStamp = $unixTimeStamp + 3600*24*$correction;
  163. return $unixTimeStamp;
  164. }
  165. /**
  166. * This will convert given Hijri date (Islamic calendar) into Gregorian date
  167. *
  168. * @param integer $Y Hijri year (Islamic calendar)
  169. * @param integer $M Hijri month (Islamic calendar)
  170. * @param integer $D Hijri day (Islamic calendar)
  171. *
  172. * @return array Gregorian date [int Year, int Month, int Day]
  173. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  174. */
  175. protected function convertDate($Y, $M, $D)
  176. {
  177. // Converts Julian Day Count to a string containing the
  178. // Gregorian date in the format of "month/day/year".
  179. // To get these functions to work, you have to compile PHP
  180. // with --enable-calendar
  181. // http://www.php.net/manual/en/calendar.installation.php
  182. //$str = JDToGregorian($this->islamic_to_jd($Y, $M, $D));
  183. $str = $this->jdToGreg($this->islamicToJd($Y, $M, $D));
  184. list($month, $day, $year) = explode('/', $str);
  185. return array($year, $month, $day);
  186. }
  187. /**
  188. * This will convert given Hijri date (Islamic calendar) into Julian day
  189. *
  190. * @param integer $year Hijri year
  191. * @param integer $month Hijri month
  192. * @param integer $day Hijri day
  193. *
  194. * @return integer Julian day
  195. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  196. */
  197. protected function islamicToJd($year, $month, $day)
  198. {
  199. $temp = ($day + ceil(29.5 * ($month - 1)) + ($year - 1) * 354 +
  200. floor((3 + (11 * $year)) / 30) + self::$_islamicEpoch) - 1;
  201. return $temp;
  202. }
  203. /**
  204. * Converts Julian Day Count to Gregorian date
  205. *
  206. * @param integer $julian A julian day number as integer
  207. *
  208. * @return integer The gregorian date as a string in the form "month/day/year"
  209. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  210. */
  211. protected function jdToGreg($julian)
  212. {
  213. $julian = $julian - 1721119;
  214. $calc1 = 4 * $julian - 1;
  215. $year = floor($calc1 / 146097);
  216. $julian = floor($calc1 - 146097 * $year);
  217. $day = floor($julian / 4);
  218. $calc2 = 4 * $day + 3;
  219. $julian = floor($calc2 / 1461);
  220. $day = $calc2 - 1461 * $julian;
  221. $day = floor(($day + 4) / 4);
  222. $calc3 = 5 * $day - 3;
  223. $month = floor($calc3 / 153);
  224. $day = $calc3 - 153 * $month;
  225. $day = floor(($day + 5) / 5);
  226. $year = 100 * $year + $julian;
  227. if ($month < 10) {
  228. $month = $month + 3;
  229. } else {
  230. $month = $month - 9;
  231. $year = $year + 1;
  232. }
  233. return $month.'/'.$day.'/'.$year;
  234. }
  235. /**
  236. * Calculate Hijri calendar correction using Um-Al-Qura calendar information
  237. *
  238. * @param integer $m Hijri month (Islamic calendar)
  239. * @param integer $y Hijri year (Islamic calendar)
  240. *
  241. * @return integer Correction factor to fix Hijri calendar calculation using
  242. * Um-Al-Qura calendar information
  243. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  244. */
  245. public function mktimeCorrection ($m, $y)
  246. {
  247. $calc = $this->mktime(0, 0, 0, $m, 1, $y);
  248. $file = dirname(__FILE__).'/data/um_alqoura.txt';
  249. $content = file_get_contents($file);
  250. $offset = (($y-1420) * 12 + $m) * 11;
  251. $d = substr($content, $offset, 2);
  252. $m = substr($content, $offset+3, 2);
  253. $y = substr($content, $offset+6, 4);
  254. $real = mktime(0, 0, 0, $m, $d, $y);
  255. $diff = (int)(($real - $calc) / (3600 * 24));
  256. return $diff;
  257. }
  258. }