. * * ---------------------------------------------------------------------- * * Class Name: Arabic StrToTime Class * * Filename: ArStrToTime.class.php * * Original Author(s): Khaled Al-Sham'aa * * Purpose: Parse about any Arabic textual datetime description into * a Unix timestamp * * ---------------------------------------------------------------------- * * Arabic StrToTime Class * * PHP class to parse about any Arabic textual datetime description into * a Unix timestamp. * * The function expects to be given a string containing an Arabic date format * and will try to parse that format into a Unix timestamp (the number of seconds * since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, or * the current time if none is supplied. * * Example: * * date_default_timezone_set('UTC'); * $time = time(); * * echo date('l dS F Y', $time); * echo '

'; * * include('./Arabic.php'); * $obj = new Arabic('ArStrToTime'); * * $int = $obj->strtotime($str, $time); * $date = date('l dS F Y', $int); * echo "Arabic String: $str
"; * echo "Unix Timestamp: $int
"; * echo "Formated Date: $date
"; *
* * @category I18N * @package Arabic * @author Khaled Al-Shamaa * @copyright 2006-2010 Khaled Al-Shamaa * * @license LGPL * @link http://www.ar-php.org */ // New in PHP V5.3: Namespaces // namespace I18N/Arabic/ArStrToTime; /** * This PHP class parse about any Arabic textual datetime description into a * Unix timestamp * * @category I18N * @package Arabic * @author Khaled Al-Shamaa * @copyright 2006-2010 Khaled Al-Shamaa * * @license LGPL * @link http://www.ar-php.org */ class ArStrToTime { protected static $hj = array(); protected static $strtotimeSearch = array(); protected static $strtotimeReplace = array(); /** * "strtotime" method input charset * @var String */ public $strtotimeInput = 'utf-8'; /** * Name of the textual "strtotime" method parameters * @var Array */ public $strtotimeVars = array('text'); /** * Loads initialize values */ public function __construct() { $xml = simplexml_load_file(dirname(__FILE__).'/data/ArStrToTime.xml'); foreach ($xml->xpath("//str_replace[@function='strtotime']/pair") as $pair) { array_push(self::$strtotimeSearch, (string)$pair->search); array_push(self::$strtotimeReplace, (string)$pair->replace); } foreach ($xml->hj_month->month as $month) { array_push(self::$hj, (string)$month); } } /** * This method will parse about any Arabic textual datetime description into * a Unix timestamp * * @param string $text The string to parse, according to the GNU » * Date Input Formats syntax (in Arabic). * @param integer $now The timestamp used to calculate the * returned value. * * @return Integer Returns a timestamp on success, FALSE otherwise * @author Khaled Al-Shamaa */ public static function strtotime($text, $now) { $int = 0; for ($i=0; $i<12; $i++) { if (strpos($text, self::$hj[$i]) > 0) { preg_match('/.*(\d{1,2}).*(\d{4}).*/', $text, $matches); include dirname(__FILE__).'/ArMktime.class.php'; $temp = new ArMktime(); $int = $temp->mktime(0, 0, 0, $i+1, $matches[1], $matches[2]); $temp = null; break; } } if ($int == 0) { $patterns = array(); $replacements = array(); array_push($patterns, '/َ|ً|ُ|ٌ|ِ|ٍ|ْ|ّ/'); array_push($replacements, ''); array_push($patterns, '/\s*ال(\S{3,})\s+ال(\S{3,})/'); array_push($replacements, ' \\2 \\1'); array_push($patterns, '/\s*ال(\S{3,})/'); array_push($replacements, ' \\1'); $text = preg_replace($patterns, $replacements, $text); $text = str_replace(self::$strtotimeSearch, self::$strtotimeReplace, $text); $pattern = '[ابتثجحخدذرزسشصضطظعغفقكلمنهوي]'; $text = preg_replace("/$pattern/", '', $text); $int = strtotime($text, $now); } return $int; } }