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

Source for file Date.php

Documentation is available at Date.php

  1. <?php
  2.  
  3. /**
  4.  * PHPExcel
  5.  *
  6.  * Copyright (c) 2006 - 2011 PHPExcel
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General Public
  19.  * License along with this library; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21.  *
  22.  * @category   PHPExcel
  23.  * @package    PHPExcel_Shared
  24.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  25.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26.  * @version    1.7.6, 2011-02-27
  27.  */
  28.  
  29.  
  30. /**
  31.  * PHPExcel_Shared_Date
  32.  *
  33.  * @category   PHPExcel
  34.  * @package    PHPExcel_Shared
  35.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  36.  */
  37. {
  38.     /** constants */
  39.     const CALENDAR_WINDOWS_1900 1900;        //    Base date of 1st Jan 1900 = 1.0
  40.     const CALENDAR_MAC_1904 1904;            //    Base date of 2nd Jan 1904 = 1.0
  41.  
  42.     private static $ExcelBaseDate    self::CALENDAR_WINDOWS_1900;
  43.  
  44.     public static $dateTimeObjectType    'DateTime';
  45.  
  46.  
  47.     /**
  48.      * Set the Excel calendar (Windows 1900 or Mac 1904)
  49.      *
  50.      * @param     integer    $baseDate            Excel base date
  51.      * @return     boolean                        Success or failure
  52.      */
  53.     public static function setExcelCalendar($baseDate{
  54.         if (($baseDate == self::CALENDAR_WINDOWS_1900||
  55.             ($baseDate == self::CALENDAR_MAC_1904)) {
  56.             self::$ExcelBaseDate $baseDate;
  57.             return True;
  58.         }
  59.         return False;
  60.     }    //    function setExcelCalendar()
  61.  
  62.  
  63.     /**
  64.      * Return the Excel calendar (Windows 1900 or Mac 1904)
  65.      *
  66.      * @return     integer    $baseDate            Excel base date
  67.      */
  68.     public static function getExcelCalendar({
  69.         return self::$ExcelBaseDate;
  70.     }    //    function getExcelCalendar()
  71.  
  72.  
  73.     /**
  74.      * Convert a date from Excel to PHP
  75.      *
  76.      * @param     long     $dateValue        Excel date/time value
  77.      * @return     long                    PHP serialized date/time
  78.      */
  79.     public static function ExcelToPHP($dateValue 0{
  80.         if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900{
  81.             $myExcelBaseDate 25569;
  82.             //    Adjust for the spurious 29-Feb-1900 (Day 60)
  83.             if ($dateValue 60{
  84.                 --$myExcelBaseDate;
  85.             }
  86.         else {
  87.             $myExcelBaseDate 24107;
  88.         }
  89.  
  90.         // Perform conversion
  91.         if ($dateValue >= 1{
  92.             $utcDays $dateValue $myExcelBaseDate;
  93.             $returnValue round($utcDays 24 60 60);
  94.             if (($returnValue <= PHP_INT_MAX&& ($returnValue >= -PHP_INT_MAX)) {
  95.                 $returnValue = (integer) $returnValue;
  96.             }
  97.         else {
  98.             $hours round($dateValue 24);
  99.             $mins round($dateValue 24 60round($hours 60);
  100.             $secs round($dateValue 24 60 60round($hours 60 60round($mins 60);
  101.             $returnValue = (integer) gmmktime($hours$mins$secs);
  102.         }
  103.  
  104.         // Return
  105.         return $returnValue;
  106.     }    //    function ExcelToPHP()
  107.  
  108.  
  109.     /**
  110.      * Convert a date from Excel to a PHP Date/Time object
  111.      *
  112.      * @param     long     $dateValue        Excel date/time value
  113.      * @return     long                    PHP date/time object
  114.      */
  115.     public static function ExcelToPHPObject($dateValue 0{
  116.         $dateTime self::ExcelToPHP($dateValue);
  117.         $days floor($dateTime 86400);
  118.         $time round((($dateTime 86400$days86400);
  119.         $hours round($time 3600);
  120.         $minutes round($time 60($hours 60);
  121.         $seconds round($time($hours 3600($minutes 60);
  122.  
  123.         $dateObj date_create('1-Jan-1970+'.$days.' days');
  124.         $dateObj->setTime($hours,$minutes,$seconds);
  125.  
  126.         return $dateObj;
  127.     }    //    function ExcelToPHPObject()
  128.  
  129.  
  130.     /**
  131.      * Convert a date from PHP to Excel
  132.      *
  133.      * @param     mixed        $dateValue    PHP serialized date/time or date object
  134.      * @return     mixed                    Excel date/time value
  135.      *                                         or boolean False on failure
  136.      */
  137.     public static function PHPToExcel($dateValue 0{
  138.         $saveTimeZone date_default_timezone_get();
  139.         date_default_timezone_set('UTC');
  140.         $retValue False;
  141.         if ((is_object($dateValue)) && ($dateValue instanceof self::$dateTimeObjectType)) {
  142.             $retValue self::FormattedPHPToExcel$dateValue->format('Y')$dateValue->format('m')$dateValue->format('d'),
  143.                                                    $dateValue->format('H')$dateValue->format('i')$dateValue->format('s')
  144.                                                  );
  145.         elseif (is_numeric($dateValue)) {
  146.             $retValue self::FormattedPHPToExceldate('Y',$dateValue)date('m',$dateValue)date('d',$dateValue),
  147.                                                    date('H',$dateValue)date('i',$dateValue)date('s',$dateValue)
  148.                                                  );
  149.         }
  150.         date_default_timezone_set($saveTimeZone);
  151.  
  152.         return $retValue;
  153.     }    //    function PHPToExcel()
  154.  
  155.  
  156.     /**
  157.      * FormattedPHPToExcel
  158.      *
  159.      * @param    long    $year 
  160.      * @param    long    $month 
  161.      * @param    long    $day 
  162.      * @param    long    $hours 
  163.      * @param    long    $minutes 
  164.      * @param    long    $seconds 
  165.      * @return  long                Excel date/time value
  166.      */
  167.     public static function FormattedPHPToExcel($year$month$day$hours=0$minutes=0$seconds=0{
  168.         if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900{
  169.             //
  170.             //    Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel
  171.             //    This affects every date following 28th February 1900
  172.             //
  173.             $excel1900isLeapYear True;
  174.             if (($year == 1900&& ($month <= 2)) $excel1900isLeapYear False}
  175.             $myExcelBaseDate 2415020;
  176.         else {
  177.             $myExcelBaseDate 2416481;
  178.             $excel1900isLeapYear False;
  179.         }
  180.  
  181.         //    Julian base date Adjustment
  182.         if ($month 2{
  183.             $month $month 3;
  184.         else {
  185.             $month $month 9;
  186.             --$year;
  187.         }
  188.  
  189.         //    Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
  190.         $century substr($year,0,2);
  191.         $decade substr($year,2,2);
  192.         $excelDate floor((146097 $century4floor((1461 $decade4floor((153 $month 25$day 1721119 $myExcelBaseDate $excel1900isLeapYear;
  193.  
  194.         $excelTime (($hours 3600($minutes 60$seconds86400;
  195.  
  196.         return (float) $excelDate $excelTime;
  197.     }    //    function FormattedPHPToExcel()
  198.  
  199.  
  200.     /**
  201.      * Is a given cell a date/time?
  202.      *
  203.      * @param     PHPExcel_Cell    $pCell 
  204.      * @return     boolean 
  205.      */
  206.     public static function isDateTime(PHPExcel_Cell $pCell{
  207.         return self::isDateTimeFormat($pCell->getParent()->getStyle($pCell->getCoordinate())->getNumberFormat());
  208.     }    //    function isDateTime()
  209.  
  210.  
  211.     /**
  212.      * Is a given number format a date/time?
  213.      *
  214.      * @param     PHPExcel_Style_NumberFormat    $pFormat 
  215.      * @return     boolean 
  216.      */
  217.     public static function isDateTimeFormat(PHPExcel_Style_NumberFormat $pFormat{
  218.         return self::isDateTimeFormatCode($pFormat->getFormatCode());
  219.     }    //    function isDateTimeFormat()
  220.  
  221.  
  222.     private static    $possibleDateFormatCharacters 'ymdHs';
  223.  
  224.     /**
  225.      * Is a given number format code a date/time?
  226.      *
  227.      * @param     string    $pFormatCode 
  228.      * @return     boolean 
  229.      */
  230.     public static function isDateTimeFormatCode($pFormatCode ''{
  231.         // Switch on formatcode
  232.         switch ($pFormatCode{
  233.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD:
  234.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2:
  235.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY:
  236.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH:
  237.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS:
  238.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS:
  239.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS:
  240.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME:
  241.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1:
  242.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2:
  243.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3:
  244.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4:
  245.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5:
  246.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6:
  247.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7:
  248.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8:
  249.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH:
  250.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14:
  251.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15:
  252.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16:
  253.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17:
  254.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22:
  255.                 return true;
  256.         }
  257.  
  258.         //    Typically number, currency or accounting (or occasionally fraction) formats
  259.         if ((substr($pFormatCode,0,1== '_'|| (substr($pFormatCode,0,2== '0 ')) {
  260.             return false;
  261.         }
  262.         // Try checking for any of the date formatting characters that don't appear within square braces
  263.         if (preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$pFormatCode)) {
  264.             //    We might also have a format mask containing quoted strings...
  265.             //        we don't want to test for any of our characters within the quoted blocks
  266.             if (strpos($pFormatCode,'"'!== false{
  267.                 $i false;
  268.                 foreach(explode('"',$pFormatCodeas $subVal{
  269.                     //    Only test in alternate array entries (the non-quoted blocks)
  270.                     if (($i !$i&& (preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$subVal))) {
  271.                         return true;
  272.                     }
  273.                 }
  274.                 return false;
  275.             }
  276.             return true;
  277.         }
  278.  
  279.         // No date...
  280.         return false;
  281.     }    //    function isDateTimeFormatCode()
  282.  
  283.  
  284.     /**
  285.      * Convert a date/time string to Excel time
  286.      *
  287.      * @param    string    $dateValue        Examples: '2009-12-31', '2009-12-31 15:59', '2009-12-31 15:59:10'
  288.      * @return    float|false       Excel date/time serial value
  289.      */
  290.     public static function stringToExcel($dateValue ''{
  291.         if (strlen($dateValue2)
  292.             return false;
  293.         if (!preg_match('/^(\d{1,4}[ \.\/\-][A-Z]{3,9}([ \.\/\-]\d{1,4})?|[A-Z]{3,9}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?|\d{1,4}[ \.\/\-]\d{1,4}([ \.\/\-]\d{1,4})?)( \d{1,2}:\d{1,2}(:\d{1,2})?)?$/iu'$dateValue))
  294.             return false;
  295.  
  296.         $dateValueNew PHPExcel_Calculation_DateTime::DATEVALUE($dateValue);
  297.  
  298.         if ($dateValueNew === PHPExcel_Calculation_Functions::VALUE()) {
  299.             return false;
  300.         else {
  301.             if (strpos($dateValue':'!== false{
  302.                 $timeValue PHPExcel_Calculation_DateTime::TIMEVALUE($dateValue);
  303.                 if ($timeValue === PHPExcel_Calculation_Functions::VALUE()) {
  304.                     return false;
  305.                 }
  306.                 $dateValueNew += $timeValue;
  307.             }
  308.             return $dateValueNew;
  309.         }
  310.  
  311.  
  312.     }
  313.  
  314. }

Documentation generated on Sun, 27 Feb 2011 16:29:22 -0800 by phpDocumentor 1.4.3