I18N_Arabic

# Source for file StrToTime.php

Documentation is available at StrToTime.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 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-2016 Khaled Al-Sham'aa
73. *
74. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
75. * @link http://www.ar-php.org
76. */
78. /**
79. * This PHP class parse about any Arabic textual datetime description into a
80. * Unix timestamp
81. *
82. * @category I18N
83. * @package I18N_Arabic
84. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
85. * @copyright 2006-2016 Khaled Al-Sham'aa
86. *
87. * @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
88. * @link http://www.ar-php.org
89. */
90. class I18N_Arabic_StrToTime
91. {
92. private static $_hj = array();
94. private static $_strtotimeSearch = array();
95. private static $_strtotimeReplace = array();
97. /**
98. * Loads initialize values
99. *
100. * @ignore
101. */
102. public function __construct()
103. {
104. $xml = simplexml_load_file(dirname(__FILE__).'/data/ArStrToTime.xml');
106. foreach ($xml->xpath("//str_replace[@function='strtotime']/pair") as $pair) {
107. array_push(self::$_strtotimeSearch, (string)$pair->search);
108. array_push(self::$_strtotimeReplace, (string)$pair->replace);
109. }
111. foreach ($xml->hj_month->month as $month) {
112. array_push(self::$_hj, (string)$month);
113. }
114. }
116. /**
117. * This method will parse about any Arabic textual datetime description into
118. * a Unix timestamp
119. *
120. * @param string $text The string to parse, according to the GNU »
121. * Date Input Formats syntax (in Arabic).
122. * @param integer $now The timestamp used to calculate the
123. * returned value.
124. *
125. * @return Integer Returns a timestamp on success, FALSE otherwise
126. * @author Khaled Al-Sham'aa <khaled@ar-php.org>
127. */
128. public static function strtotime($text, $now)
129. {
130. $int = 0;
132. for ($i=0; $i<12; $i++) {
133. if (strpos($text, self::$_hj[$i]) > 0) {
134. preg_match('/.*(\d{1,2}).*(\d{4}).*/', $text, $matches);
136. include dirname(__FILE__).DIRECTORY_SEPARATOR.'Mktime.php';
137. $temp = new I18N_Arabic_Mktime();
138. $fix = $temp->mktimeCorrection($i+1, $matches[2]);
139. $int = $temp->mktime(0, 0, 0, $i+1, $matches[1], $matches[2], $fix);
140. $temp = null;
142. break;
143. }
144. }
146. if ($int == 0) {
147. $patterns = array();
148. $replacements = array();
150. array_push($patterns, '/َ|ً|ُ|ٌ|ِ|ٍ|ْ|ّ/');
151. array_push($replacements, '');
153. array_push($patterns, '/\s*ال(\S{3,})\s+ال(\S{3,})/');
154. array_push($replacements, ' \\2 \\1');
156. array_push($patterns, '/\s*ال(\S{3,})/');
157. array_push($replacements, ' \\1');
159. $text = preg_replace($patterns, $replacements, $text);
160. $text = str_replace(
161. self::$_strtotimeSearch,
162. self::$_strtotimeReplace,
163. $text
164. );
166. $pattern = '[ابتثجحخدذرزسشصضطظعغفقكلمنهوي]';
167. $text = preg_replace("/$pattern/", '', $text);
169. $int = strtotime($text, $now);
170. }
172. return $int;
173. }
174. }

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