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

Source for file Mktime.php

Documentation is available at Mktime.php

  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.  
  109. // New in PHP V5.3: Namespaces
  110. // namespace I18N\Arabic;
  111. // 
  112. // $obj = new I18N\Arabic\Mktime();
  113. // 
  114. // use I18N\Arabic;
  115. // $obj = new Arabic\Mktime();
  116. //
  117. // use I18N\Arabic\Mktime as Mktime;
  118. // $obj = new Mktime();
  119.  
  120. /**
  121.  * This PHP class is an Arabic customization for PHP mktime function
  122.  *  
  123.  * @category  I18N
  124.  * @package   I18N_Arabic
  125.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  126.  * @copyright 2006-2012 Khaled Al-Sham'aa
  127.  *    
  128.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  129.  * @link      http://www.ar-php.org
  130.  */ 
  131. {
  132.     private static $_islamicEpoch 1948439.5;
  133.  
  134.     /**
  135.      * Loads initialize values
  136.      *
  137.      * @ignore
  138.      */
  139.     public function __construct()
  140.     {
  141.     }
  142.         
  143.     /**
  144.      * This will return current Unix timestamp
  145.      * for given Hijri date (Islamic calendar)
  146.      *          
  147.      * @param integer $hour       Time hour
  148.      * @param integer $minute     Time minute
  149.      * @param integer $second     Time second
  150.      * @param integer $hj_month   Hijri month (Islamic calendar)
  151.      * @param integer $hj_day     Hijri day   (Islamic calendar)
  152.      * @param integer $hj_year    Hijri year  (Islamic calendar)
  153.      * @param integer $correction To apply correction factor (+/- 1-2)
  154.      *                             to standard Hijri calendar
  155.      *             
  156.      * @return integer Returns the current time measured in the number of
  157.      *                 seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  158.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  159.      */
  160.     public function mktime($hour$minute$second
  161.                           $hj_month$hj_day$hj_year$correction 0)
  162.     {
  163.         list($year$month$day$this->convertDate($hj_year$hj_month$hj_day);
  164.  
  165.         $unixTimeStamp mktime($hour$minute$second$month$day$year);
  166.         
  167.         $unixTimeStamp $unixTimeStamp 3600*24*$correction
  168.         
  169.         return $unixTimeStamp;
  170.     }
  171.     
  172.     /**
  173.      * This will convert given Hijri date (Islamic calendar) into Gregorian date
  174.      *          
  175.      * @param integer $Y Hijri year (Islamic calendar)
  176.      * @param integer $M Hijri month (Islamic calendar)
  177.      * @param integer $D Hijri day (Islamic calendar)
  178.      *      
  179.      * @return array Gregorian date [int Year, int Month, int Day]
  180.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  181.      */
  182.     protected function convertDate($Y$M$D)
  183.     {
  184.         // Converts Julian Day Count to a string containing the
  185.         // Gregorian date in the format of "month/day/year".
  186.         
  187.         // To get these functions to work, you have to compile PHP 
  188.         // with --enable-calendar 
  189.         // http://www.php.net/manual/en/calendar.installation.php
  190.         //$str = JDToGregorian($this->islamic_to_jd($Y, $M, $D));
  191.         
  192.         $str $this->jdToGreg($this->islamicToJd($Y$M$D));
  193.         
  194.         list($month$day$yearexplode('/'$str);
  195.         
  196.         return array($year$month$day);
  197.     }
  198.     
  199.     /**
  200.      * This will convert given Hijri date (Islamic calendar) into Julian day
  201.      *          
  202.      * @param integer $year  Hijri year
  203.      * @param integer $month Hijri month
  204.      * @param integer $day   Hijri day
  205.      *      
  206.      * @return integer Julian day
  207.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  208.      */
  209.     protected function islamicToJd($year$month$day)
  210.     {
  211.         $temp ($day ceil(29.5 ($month 1)) ($year 1354 
  212.                  floor(((11 $year)) 30self::$_islamicEpoch1;
  213.  
  214.         return $temp
  215.     }
  216.     
  217.     /**
  218.      * Converts Julian Day Count to Gregorian date
  219.      *      
  220.      * @param integer $julian A julian day number as integer
  221.      *       
  222.      * @return integer The gregorian date as a string in the form "month/day/year"
  223.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  224.      */
  225.     protected function jdToGreg($julian
  226.     {
  227.         $julian $julian 1721119;
  228.         $calc1  $julian 1;
  229.         $year   floor($calc1 146097);
  230.         $julian floor($calc1 146097 $year);
  231.         $day    floor($julian 4);
  232.         $calc2  $day 3;
  233.         $julian floor($calc2 1461);
  234.         $day    $calc2 1461 $julian;
  235.         $day    floor(($day 44);
  236.         $calc3  $day 3;
  237.         $month  floor($calc3 153);
  238.         $day    $calc3 153 $month;
  239.         $day    floor(($day 55);
  240.         $year   100 $year $julian;
  241.         
  242.         if ($month 10{
  243.             $month $month 3;
  244.         else {
  245.             $month $month 9;
  246.             $year  $year 1;
  247.         }
  248.  
  249.         return $month.'/'.$day.'/'.$year;
  250.     }
  251.  
  252.     /**
  253.      * Calculate Hijri calendar correction using Um-Al-Qura calendar information
  254.      *      
  255.      * @param integer $m Hijri month (Islamic calendar)
  256.      * @param integer $y Hijri year  (Islamic calendar)
  257.      *       
  258.      * @return integer Correction factor to fix Hijri calendar calculation using
  259.      *                  Um-Al-Qura calendar information
  260.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  261.      */
  262.     public function mktimeCorrection ($m$y)
  263.     {
  264.         $calc $this->mktime(000$m1$y);
  265.         
  266.         $file dirname(__FILE__).'/data/um_alqoura.txt';
  267.  
  268.         $content file_get_contents($file);
  269.         $offset  (($y-142012 $m11;
  270.         
  271.         $d substr($content$offset2);
  272.         $m substr($content$offset+32);
  273.         $y substr($content$offset+64);
  274.         
  275.         $real mktime(000$m$d$y);
  276.         
  277.         $diff = (int)(($real $calc(3600 24));
  278.         
  279.         return $diff;
  280.     }
  281. }

Documentation generated on Wed, 29 Aug 2012 08:33:01 +0200 by phpDocumentor 1.4.0