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 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  25.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26.  * @version    1.7.2, 2010-01-11
  27.  */
  28.  
  29.  
  30. /** PHPExcel root directory */
  31. if (!defined('PHPEXCEL_ROOT')) {
  32.     /**
  33.      * @ignore
  34.      */
  35.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  36. }
  37.  
  38. /** PHPExcel_Cell */
  39. require_once PHPEXCEL_ROOT 'PHPExcel/Cell.php';
  40.  
  41. /** PHPExcel_Style_NumberFormat */
  42. require_once PHPEXCEL_ROOT 'PHPExcel/Style/NumberFormat.php';
  43.  
  44.  
  45. /**
  46.  * PHPExcel_Shared_Date
  47.  *
  48.  * @category   PHPExcel
  49.  * @package    PHPExcel_Shared
  50.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  51.  */
  52. {
  53.     /** constants */
  54.     const CALENDAR_WINDOWS_1900 1900;        //    Base date of 1st Jan 1900 = 1.0
  55.     const CALENDAR_MAC_1904 1904;            //    Base date of 2nd Jan 1904 = 1.0
  56.  
  57.     private static $ExcelBaseDate    self::CALENDAR_WINDOWS_1900;
  58.  
  59.     public static $dateTimeObjectType    'DateTime';
  60.  
  61.  
  62.     /**
  63.      * Set the Excel calendar (Windows 1900 or Mac 1904)
  64.      *
  65.      * @param     integer    $baseDate            Excel base date
  66.      * @return     boolean                        Success or failure
  67.      */
  68.     public static function setExcelCalendar($baseDate{
  69.         if (($baseDate == self::CALENDAR_WINDOWS_1900||
  70.             ($baseDate == self::CALENDAR_MAC_1904)) {
  71.             self::$ExcelBaseDate $baseDate;
  72.             return True;
  73.         }
  74.         return False;
  75.     }    //    function setExcelCalendar()
  76.  
  77.  
  78.     /**
  79.      * Return the Excel calendar (Windows 1900 or Mac 1904)
  80.      *
  81.      * @return     integer    $baseDate            Excel base date
  82.      */
  83.     public static function getExcelCalendar({
  84.         return self::$ExcelBaseDate;
  85.     }    //    function getExcelCalendar()
  86.  
  87.  
  88.     /**
  89.      * Convert a date from Excel to PHP
  90.      *
  91.      * @param     long     $dateValue        Excel date/time value
  92.      * @return     long                    PHP serialized date/time
  93.      */
  94.     public static function ExcelToPHP($dateValue 0{
  95.         if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900{
  96.             $myExcelBaseDate 25569;
  97.             //    Adjust for the spurious 29-Feb-1900 (Day 60)
  98.             if ($dateValue 60{
  99.                 --$myExcelBaseDate;
  100.             }
  101.         else {
  102.             $myExcelBaseDate 24107;
  103.         }
  104.  
  105.         // Perform conversion
  106.         if ($dateValue >= 1{
  107.             $utcDays $dateValue $myExcelBaseDate;
  108.             $returnValue round($utcDays 24 60 60);
  109.             if (($returnValue <= PHP_INT_MAX&& ($returnValue >= -PHP_INT_MAX)) {
  110.                 $returnValue = (integer) $returnValue;
  111.             }
  112.         else {
  113.             $hours round($dateValue 24);
  114.             $mins round($dateValue 24 60round($hours 60);
  115.             $secs round($dateValue 24 60 60round($hours 60 60round($mins 60);
  116.             $returnValue = (integer) gmmktime($hours$mins$secs);
  117.         }
  118.  
  119.         // Return
  120.         return $returnValue;
  121.     }    //    function ExcelToPHP()
  122.  
  123.  
  124.     /**
  125.      * Convert a date from Excel to a PHP Date/Time object
  126.      *
  127.      * @param     long     $dateValue        Excel date/time value
  128.      * @return     long                    PHP date/time object
  129.      */
  130.     public static function ExcelToPHPObject($dateValue 0{
  131.         $dateTime self::ExcelToPHP($dateValue);
  132.         $days floor($dateTime 86400);
  133.         $time round((($dateTime 86400$days86400);
  134.         $hours round($time 3600);
  135.         $minutes round($time 60($hours 60);
  136.         $seconds round($time($hours 3600($minutes 60);
  137.  
  138.         $dateObj date_create('1-Jan-1970+'.$days.' days');
  139.         $dateObj->setTime($hours,$minutes,$seconds);
  140.  
  141.         return $dateObj;
  142.     }    //    function ExcelToPHPObject()
  143.  
  144.  
  145.     /**
  146.      * Convert a date from PHP to Excel
  147.      *
  148.      * @param     mixed        $dateValue    PHP serialized date/time or date object
  149.      * @return     mixed                    Excel date/time value
  150.      *                                         or boolean False on failure
  151.      */
  152.     public static function PHPToExcel($dateValue 0{
  153.         $saveTimeZone date_default_timezone_get();
  154.         date_default_timezone_set('UTC');
  155.         $retValue False;
  156.         if ((is_object($dateValue)) && ($dateValue instanceof self::$dateTimeObjectType)) {
  157.             $retValue self::FormattedPHPToExcel$dateValue->format('Y')$dateValue->format('m')$dateValue->format('d'),
  158.                                                    $dateValue->format('H')$dateValue->format('i')$dateValue->format('s')
  159.                                                  );
  160.         elseif (is_numeric($dateValue)) {
  161.             $retValue self::FormattedPHPToExceldate('Y',$dateValue)date('m',$dateValue)date('d',$dateValue),
  162.                                                    date('H',$dateValue)date('i',$dateValue)date('s',$dateValue)
  163.                                                  );
  164.         }
  165.         date_default_timezone_set($saveTimeZone);
  166.  
  167.         return $retValue;
  168.     }    //    function PHPToExcel()
  169.  
  170.  
  171.     /**
  172.      * FormattedPHPToExcel
  173.      *
  174.      * @param    long    $year 
  175.      * @param    long    $month 
  176.      * @param    long    $day 
  177.      * @param    long    $hours 
  178.      * @param    long    $minutes 
  179.      * @param    long    $seconds 
  180.      * @return  long                Excel date/time value
  181.      */
  182.     public static function FormattedPHPToExcel($year$month$day$hours=0$minutes=0$seconds=0{
  183.         if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900{
  184.             //
  185.             //    Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel
  186.             //    This affects every date following 28th February 1900
  187.             //
  188.             $excel1900isLeapYear True;
  189.             if (($year == 1900&& ($month <= 2)) $excel1900isLeapYear False}
  190.             $myExcelBaseDate 2415020;
  191.         else {
  192.             $myExcelBaseDate 2416481;
  193.             $excel1900isLeapYear False;
  194.         }
  195.  
  196.         //    Julian base date Adjustment
  197.         if ($month 2{
  198.             $month $month 3;
  199.         else {
  200.             $month $month 9;
  201.             --$year;
  202.         }
  203.  
  204.         //    Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
  205.         $century substr($year,0,2);
  206.         $decade substr($year,2,2);
  207.         $excelDate floor((146097 $century4floor((1461 $decade4floor((153 $month 25$day 1721119 $myExcelBaseDate $excel1900isLeapYear;
  208.  
  209.         $excelTime (($hours 3600($minutes 60$seconds86400;
  210.  
  211.         return (float) $excelDate $excelTime;
  212.     }    //    function FormattedPHPToExcel()
  213.  
  214.  
  215.     /**
  216.      * Is a given cell a date/time?
  217.      *
  218.      * @param     PHPExcel_Cell    $pCell 
  219.      * @return     boolean 
  220.      */
  221.     public static function isDateTime(PHPExcel_Cell $pCell{
  222.         return self::isDateTimeFormat($pCell->getParent()->getStyle($pCell->getCoordinate())->getNumberFormat());
  223.     }    //    function isDateTime()
  224.  
  225.  
  226.     /**
  227.      * Is a given number format a date/time?
  228.      *
  229.      * @param     PHPExcel_Style_NumberFormat    $pFormat 
  230.      * @return     boolean 
  231.      */
  232.     public static function isDateTimeFormat(PHPExcel_Style_NumberFormat $pFormat{
  233.         return self::isDateTimeFormatCode($pFormat->getFormatCode());
  234.     }    //    function isDateTimeFormat()
  235.  
  236.  
  237.     private static    $possibleDateFormatCharacters 'ymdHis';
  238.  
  239.     /**
  240.      * Is a given number format code a date/time?
  241.      *
  242.      * @param     string    $pFormatCode 
  243.      * @return     boolean 
  244.      */
  245.     public static function isDateTimeFormatCode($pFormatCode ''{
  246.         // Switch on formatcode
  247.         switch ($pFormatCode{
  248.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD:
  249.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2:
  250.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY:
  251.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH:
  252.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS:
  253.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS:
  254.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS:
  255.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME:
  256.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1:
  257.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2:
  258.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3:
  259.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4:
  260.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5:
  261.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6:
  262.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7:
  263.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8:
  264.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH:
  265.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14:
  266.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15:
  267.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16:
  268.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17:
  269.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22:
  270.                 return true;
  271.         }
  272.  
  273.         // Try checking for any of the date formatting characters that don't appear within square braces
  274.         if (preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$pFormatCode)) {
  275.             return true;
  276.         }
  277.  
  278.         // No date...
  279.         return false;
  280.     }    //    function isDateTimeFormatCode()
  281. }

Documentation generated on Mon, 11 Jan 2010 08:07:41 +0100 by phpDocumentor 1.4.1