I18N_Arabic
[ class tree: I18N_Arabic ] [ index: I18N_Arabic ] [ all elements ]

Source for file StrToTime.php

Documentation is available at StrToTime.php

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

Documentation generated on Mon, 14 Jan 2013 17:49:08 +0100 by phpDocumentor 1.4.0