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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  25.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26.  * @version    1.7.0, 2009-08-10
  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 - 2009 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 = (integer) round($utcDays 24 60 60);
  109.         else {
  110.             $hours round($dateValue 24);
  111.             $mins round($dateValue 24 60round($hours 60);
  112.             $secs round($dateValue 24 60 60round($hours 60 60round($mins 60);
  113.             $returnValue = (integer) mktime($hours$mins$secs);
  114.         }
  115.  
  116.         // Return
  117.         return $returnValue;
  118.     }    //    function ExcelToPHP()
  119.  
  120.  
  121.     /**
  122.      * Convert a date from Excel to a PHP Date/Time object
  123.      *
  124.      * @param     long     $dateValue        Excel date/time value
  125.      * @return     long                    PHP date/time object
  126.      */
  127.     public static function ExcelToPHPObject($dateValue 0{
  128.         $dateTime self::ExcelToPHP($dateValue);
  129.         $days floor($dateTime 86400);
  130.         $time round((($dateTime 86400$days86400);
  131.         $hours round($time 3600);
  132.         $minutes round($time 60($hours 60);
  133.         $seconds round($time($hours 3600($minutes 60);
  134.         $dateObj date_create('1-Jan-1970+'.$days.' days');
  135.         $dateObj->setTime($hours,$minutes,$seconds);
  136.         return $dateObj;
  137.     }    //    function ExcelToPHPObject()
  138.  
  139.  
  140.     /**
  141.      * Convert a date from PHP to Excel
  142.      *
  143.      * @param     mixed        $dateValue    PHP serialized date/time or date object
  144.      * @return     mixed                    Excel date/time value
  145.      *                                         or boolean False on failure
  146.      */
  147.     public static function PHPToExcel($dateValue 0{
  148.         $saveTimeZone date_default_timezone_get();
  149.         date_default_timezone_set('UTC');
  150.         $retValue False;
  151.         if ((is_object($dateValue)) && ($dateValue instanceof self::$dateTimeObjectType)) {
  152.             $retValue self::FormattedPHPToExcel$dateValue->format('Y')$dateValue->format('m')$dateValue->format('d'),
  153.                                                    $dateValue->format('H')$dateValue->format('i')$dateValue->format('s')
  154.                                                  );
  155.         elseif (is_numeric($dateValue)) {
  156.             $retValue self::FormattedPHPToExceldate('Y',$dateValue)date('m',$dateValue)date('d',$dateValue),
  157.                                                    date('H',$dateValue)date('i',$dateValue)date('s',$dateValue)
  158.                                                  );
  159.         }
  160.         date_default_timezone_set($saveTimeZone);
  161.  
  162.         return $retValue;
  163.     }    //    function PHPToExcel()
  164.  
  165.  
  166.     /**
  167.      * FormattedPHPToExcel
  168.      *
  169.      * @param    long    $year 
  170.      * @param    long    $month 
  171.      * @param    long    $day 
  172.      * @param    long    $hours 
  173.      * @param    long    $minutes 
  174.      * @param    long    $seconds 
  175.      * @return  long                Excel date/time value
  176.      */
  177.     public static function FormattedPHPToExcel($year$month$day$hours=0$minutes=0$seconds=0{
  178.         if (self::$ExcelBaseDate == self::CALENDAR_WINDOWS_1900{
  179.             //
  180.             //    Fudge factor for the erroneous fact that the year 1900 is treated as a Leap Year in MS Excel
  181.             //    This affects every date following 28th February 1900
  182.             //
  183.             $excel1900isLeapYear True;
  184.             if (($year == 1900&& ($month <= 2)) $excel1900isLeapYear False}
  185.             $myExcelBaseDate 2415020;
  186.         else {
  187.             $myExcelBaseDate 2416481;
  188.             $excel1900isLeapYear False;
  189.         }
  190.  
  191.         //    Julian base date Adjustment
  192.         if ($month 2{
  193.             $month $month 3;
  194.         else {
  195.             $month $month 9;
  196.             --$year;
  197.         }
  198.  
  199.         //    Calculate the Julian Date, then subtract the Excel base date (JD 2415020 = 31-Dec-1899 Giving Excel Date of 0)
  200.         $century substr($year,0,2);
  201.         $decade substr($year,2,2);
  202.         $excelDate floor((146097 $century4floor((1461 $decade4floor((153 $month 25$day 1721119 $myExcelBaseDate $excel1900isLeapYear;
  203.  
  204.         $excelTime (($hours 3600($minutes 60$seconds86400;
  205.  
  206.         return (float) $excelDate $excelTime;
  207.     }    //    function FormattedPHPToExcel()
  208.  
  209.  
  210.     /**
  211.      * Is a given cell a date/time?
  212.      *
  213.      * @param     PHPExcel_Cell    $pCell 
  214.      * @return     boolean 
  215.      */
  216.     public static function isDateTime(PHPExcel_Cell $pCell{
  217.         return self::isDateTimeFormat($pCell->getParent()->getStyle($pCell->getCoordinate())->getNumberFormat());
  218.     }    //    function isDateTime()
  219.  
  220.  
  221.     /**
  222.      * Is a given number format a date/time?
  223.      *
  224.      * @param     PHPExcel_Style_NumberFormat    $pFormat 
  225.      * @return     boolean 
  226.      */
  227.     public static function isDateTimeFormat(PHPExcel_Style_NumberFormat $pFormat{
  228.         return self::isDateTimeFormatCode($pFormat->getFormatCode());
  229.     }    //    function isDateTimeFormat()
  230.  
  231.  
  232.     private static    $possibleDateFormatCharacters 'ymdHis';
  233.  
  234.     /**
  235.      * Is a given number format code a date/time?
  236.      *
  237.      * @param     string    $pFormatCode 
  238.      * @return     boolean 
  239.      */
  240.     public static function isDateTimeFormatCode($pFormatCode ''{
  241.         // Switch on formatcode
  242.         switch ($pFormatCode{
  243.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD:
  244.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2:
  245.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY:
  246.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH:
  247.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS:
  248.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS:
  249.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS:
  250.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME:
  251.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1:
  252.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2:
  253.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3:
  254.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4:
  255.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5:
  256.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6:
  257.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7:
  258.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8:
  259.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH:
  260.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14:
  261.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15:
  262.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16:
  263.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17:
  264.             case PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22:
  265.                 return true;
  266.         }
  267.  
  268.         // Try checking for any of the date formatting characters that don't appear within square braces
  269.         if (preg_match('/(^|\])[^\[]*['.self::$possibleDateFormatCharacters.']/i',$pFormatCode)) {
  270.             return true;
  271.         }
  272.  
  273.         // No date...
  274.         return false;
  275.     }    //    function isDateTimeFormatCode()
  276. }

Documentation generated on Mon, 10 Aug 2009 08:03:04 +0200 by phpDocumentor 1.4.1