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-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 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-2013 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-2013 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.     /**
  133.      * Loads initialize values
  134.      *
  135.      * @ignore
  136.      */
  137.     public function __construct()
  138.     {
  139.     }
  140.         
  141.     /**
  142.      * This will return current Unix timestamp
  143.      * for given Hijri date (Islamic calendar)
  144.      *          
  145.      * @param integer $hour       Time hour
  146.      * @param integer $minute     Time minute
  147.      * @param integer $second     Time second
  148.      * @param integer $hj_month   Hijri month (Islamic calendar)
  149.      * @param integer $hj_day     Hijri day   (Islamic calendar)
  150.      * @param integer $hj_year    Hijri year  (Islamic calendar)
  151.      * @param integer $correction To apply correction factor (+/- 1-2)
  152.      *                             to standard Hijri calendar
  153.      *             
  154.      * @return integer Returns the current time measured in the number of
  155.      *                 seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  156.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  157.      */
  158.     public function mktime(
  159.         $hour$minute$second$hj_month$hj_day$hj_year$correction 0
  160.     {
  161.         list($year$month$day$this->convertDate($hj_year$hj_month$hj_day);
  162.  
  163.         $unixTimeStamp mktime($hour$minute$second$month$day$year);
  164.         
  165.         $unixTimeStamp $unixTimeStamp 3600*24*$correction
  166.         
  167.         return $unixTimeStamp;
  168.     }
  169.     
  170.     /**
  171.      * This will convert given Hijri date (Islamic calendar) into Gregorian date
  172.      *          
  173.      * @param integer $Y Hijri year (Islamic calendar)
  174.      * @param integer $M Hijri month (Islamic calendar)
  175.      * @param integer $D Hijri day (Islamic calendar)
  176.      *      
  177.      * @return array Gregorian date [int Year, int Month, int Day]
  178.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  179.      */
  180.     protected function convertDate($Y$M$D)
  181.     {
  182.         if (function_exists('GregorianToJD')) {
  183.             $str JDToGregorian($this->islamicToJd($Y$M$D));
  184.         else {
  185.             $str $this->jdToGreg($this->islamicToJd($Y$M$D));
  186.         }
  187.         
  188.         list($month$day$yearexplode('/'$str);
  189.         
  190.         return array($year$month$day);
  191.     }
  192.     
  193.     /**
  194.      * This will convert given Hijri date (Islamic calendar) into Julian day
  195.      *          
  196.      * @param integer $year  Hijri year
  197.      * @param integer $month Hijri month
  198.      * @param integer $day   Hijri day
  199.      *      
  200.      * @return integer Julian day
  201.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  202.      */
  203.     protected function islamicToJd($year$month$day)
  204.     {
  205.         $jd = (int)((11 $year 330+ (int)(354 $year+ (int)(30 $month
  206.             - (int)(($month 12$day 1948440 385;
  207.         return $jd;
  208.     }
  209.     
  210.     /**
  211.      * Converts Julian Day Count to Gregorian date
  212.      *      
  213.      * @param integer $julian A julian day number as integer
  214.      *       
  215.      * @return integer The gregorian date as a string in the form "month/day/year"
  216.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  217.      */
  218.     protected function jdToGreg($julian
  219.     {
  220.         $julian $julian 1721119;
  221.         $calc1  $julian 1;
  222.         $year   floor($calc1 146097);
  223.         $julian floor($calc1 146097 $year);
  224.         $day    floor($julian 4);
  225.         $calc2  $day 3;
  226.         $julian floor($calc2 1461);
  227.         $day    $calc2 1461 $julian;
  228.         $day    floor(($day 44);
  229.         $calc3  $day 3;
  230.         $month  floor($calc3 153);
  231.         $day    $calc3 153 $month;
  232.         $day    floor(($day 55);
  233.         $year   100 $year $julian;
  234.         
  235.         if ($month 10{
  236.             $month $month 3;
  237.         else {
  238.             $month $month 9;
  239.             $year  $year 1;
  240.         }
  241.         
  242.         /* 
  243.         Just to mimic the PHP JDToGregorian output
  244.         If year is less than 1, subtract one to convert from
  245.         a zero based date system to the common era system in
  246.         which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.)
  247.         */
  248.         
  249.         if ($year 1{
  250.             $year--;
  251.         }
  252.  
  253.         return $month.'/'.$day.'/'.$year;
  254.     }
  255.  
  256.     /**
  257.      * Calculate Hijri calendar correction using Um-Al-Qura calendar information
  258.      *      
  259.      * @param integer $m Hijri month (Islamic calendar)
  260.      * @param integer $y Hijri year  (Islamic calendar), valid range [1420-1459]
  261.      *       
  262.      * @return integer Correction factor to fix Hijri calendar calculation using
  263.      *                  Um-Al-Qura calendar information
  264.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  265.      */
  266.     public function mktimeCorrection ($m$y)
  267.     {
  268.         if ($y >= 1420 && $y 1460{
  269.             $calc $this->mktime(000$m1$y);
  270.             $file dirname(__FILE__).'/data/um_alqoura.txt';
  271.  
  272.             $content file_get_contents($file);
  273.             $offset  (($y-142012 $m11;
  274.  
  275.             $d substr($content$offset2);
  276.             $m substr($content$offset+32);
  277.             $y substr($content$offset+64);
  278.             
  279.             $real mktime(000$m$d$y);
  280.             
  281.             $diff = (int)(($real $calc(3600 24));
  282.         else {
  283.             $diff 0;
  284.         }
  285.         
  286.         return $diff;
  287.     }
  288.     
  289.     /**
  290.      * Calculate how many days in a given Hijri month
  291.      *      
  292.      * @param integer $m         Hijri month (Islamic calendar)
  293.      * @param integer $y         Hijri year  (Islamic calendar), valid
  294.      *                            range[1320-1459]
  295.      * @param boolean $umAlqoura Should we implement Um-Al-Qura calendar correction
  296.      *                            in this calculation (default value is true)
  297.      *       
  298.      * @return integer Days in a given Hijri month
  299.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  300.      */
  301.     public function hijriMonthDays ($m$y$umAlqoura true)
  302.     {
  303.         if ($y >= 1320 && $y 1460{
  304.             $begin $this->mktime(000$m1$y);
  305.             
  306.             if ($m == 12{
  307.                 $m2 1;
  308.                 $y2 $y 1;
  309.             else {
  310.                 $m2 $m 1;
  311.                 $y2 $y;
  312.             }
  313.             
  314.             $end $this->mktime(000$m21$y2);
  315.             
  316.             if ($umAlqoura === true{
  317.                 $c1 $this->mktimeCorrection($m$y);
  318.                 $c2 $this->mktimeCorrection($m2$y2);
  319.             else {
  320.                 $c1 0;
  321.                 $c2 0;
  322.             }
  323.             
  324.             $days ($end $begin(3600 24);
  325.             $days $days $c1 $c2;
  326.         else {
  327.             $days false;
  328.         }
  329.         
  330.         return $days;
  331.     }
  332. }

Documentation generated on Mon, 14 Jan 2013 17:48:52 +0100 by phpDocumentor 1.4.0