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

Documentation generated on Mon, 05 Jan 2009 20:36:47 +0100 by phpDocumentor 1.4.1