PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sammousa/valuematchbv-ls2
PHP | 183 lines | 50 code | 19 blank | 114 comment | 4 complexity | e35b8faf542f46af313795bd9311c627 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 StrToTime Class
  31. *
  32. * Filename: StrToTime.php
  33. *
  34. * Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35. *
  36. * Purpose: Parse about any Arabic textual datetime description into
  37. * a Unix timestamp
  38. *
  39. * ----------------------------------------------------------------------
  40. *
  41. * Arabic StrToTime Class
  42. *
  43. * PHP class to parse about any Arabic textual datetime description into
  44. * a Unix timestamp.
  45. *
  46. * The function expects to be given a string containing an Arabic date format
  47. * and will try to parse that format into a Unix timestamp (the number of seconds
  48. * since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, or
  49. * the current time if none is supplied.
  50. *
  51. * Example:
  52. * <code>
  53. * date_default_timezone_set('UTC');
  54. * $time = time();
  55. *
  56. * echo date('l dS F Y', $time);
  57. * echo '<br /><br />';
  58. *
  59. * include('./I18N/Arabic.php');
  60. * $obj = new I18N_Arabic('StrToTime');
  61. *
  62. * $int = $obj->strtotime($str, $time);
  63. * $date = date('l dS F Y', $int);
  64. * echo "<b><font color=#FFFF00>Arabic String:</font></b> $str<br />";
  65. * echo "<b><font color=#FFFF00>Unix Timestamp:</font></b> $int<br />";
  66. * echo "<b><font color=#FFFF00>Formated Date:</font></b> $date<br />";
  67. * </code>
  68. *
  69. * @category I18N
  70. * @package I18N_Arabic
  71. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  72. * @copyright 2006-2012 Khaled Al-Sham'aa
  73. *
  74. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
  75. * @link http://www.ar-php.org
  76. */
  77. // New in PHP V5.3: Namespaces
  78. // namespace I18N\Arabic;
  79. //
  80. // $obj = new I18N\Arabic\StrToTime();
  81. //
  82. // use I18N\Arabic;
  83. // $obj = new Arabic\StrToTime();
  84. //
  85. // use I18N\Arabic\StrToTime as StrToTime;
  86. // $obj = new StrToTime();
  87. /**
  88. * This PHP class parse about any Arabic textual datetime description into a
  89. * Unix timestamp
  90. *
  91. * @category I18N
  92. * @package I18N_Arabic
  93. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  94. * @copyright 2006-2012 Khaled Al-Sham'aa
  95. *
  96. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
  97. * @link http://www.ar-php.org
  98. */
  99. class I18N_Arabic_StrToTime
  100. {
  101. private static $_hj = array();
  102. private static $_strtotimeSearch = array();
  103. private static $_strtotimeReplace = array();
  104. /**
  105. * Loads initialize values
  106. *
  107. * @ignore
  108. */
  109. public function __construct()
  110. {
  111. $xml = simplexml_load_file(dirname(__FILE__).'/data/ArStrToTime.xml');
  112. foreach ($xml->xpath("//str_replace[@function='strtotime']/pair") as $pair) {
  113. array_push(self::$_strtotimeSearch, (string)$pair->search);
  114. array_push(self::$_strtotimeReplace, (string)$pair->replace);
  115. }
  116. foreach ($xml->hj_month->month as $month) {
  117. array_push(self::$_hj, (string)$month);
  118. }
  119. }
  120. /**
  121. * This method will parse about any Arabic textual datetime description into
  122. * a Unix timestamp
  123. *
  124. * @param string $text The string to parse, according to the GNU »
  125. * Date Input Formats syntax (in Arabic).
  126. * @param integer $now The timestamp used to calculate the
  127. * returned value.
  128. *
  129. * @return Integer Returns a timestamp on success, FALSE otherwise
  130. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  131. */
  132. public static function strtotime($text, $now)
  133. {
  134. $int = 0;
  135. for ($i=0; $i<12; $i++) {
  136. if (strpos($text, self::$_hj[$i]) > 0) {
  137. preg_match('/.*(\d{1,2}).*(\d{4}).*/', $text, $matches);
  138. include dirname(__FILE__).'/Mktime.php';
  139. $temp = new I18N_Arabic_Mktime();
  140. $fix = $temp->mktimeCorrection($i+1, $matches[2]);
  141. $int = $temp->mktime(0, 0, 0, $i+1, $matches[1], $matches[2], $fix);
  142. $temp = null;
  143. break;
  144. }
  145. }
  146. if ($int == 0) {
  147. $patterns = array();
  148. $replacements = array();
  149. array_push($patterns, '/َ|ً|ُ|ٌ|ِ|ٍ|ْ|ّ/');
  150. array_push($replacements, '');
  151. array_push($patterns, '/\s*ال(\S{3,})\s+ال(\S{3,})/');
  152. array_push($replacements, ' \\2 \\1');
  153. array_push($patterns, '/\s*ال(\S{3,})/');
  154. array_push($replacements, ' \\1');
  155. $text = preg_replace($patterns, $replacements, $text);
  156. $text = str_replace(self::$_strtotimeSearch,
  157. self::$_strtotimeReplace, $text);
  158. $pattern = '[ابتثجحخدذرزسشصضطظعغفقكلمنهوي]';
  159. $text = preg_replace("/$pattern/", '', $text);
  160. $int = strtotime($text, $now);
  161. }
  162. return $int;
  163. }
  164. }