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

Source for file DateTime.php

Documentation is available at DateTime.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  *
  21.  * @category    PHPExcel
  22.  * @package        PHPExcel_Calculation
  23.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version        1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  35.     require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  36. }
  37.  
  38.  
  39. /**
  40.  * PHPExcel_Calculation_DateTime
  41.  *
  42.  * @category    PHPExcel
  43.  * @package        PHPExcel_Calculation
  44.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  45.  */
  46.  
  47.     public static function _isLeapYear($year) {
  48.         return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
  49.     }    //    function _isLeapYear()
  50.  
  51.  
  52.     private static function _dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS) {
  53.         if ($startDay == 31) {
  54.             --$startDay;
  55.         } elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::_isLeapYear($startYear))))) {
  56.             $startDay = 30;
  57.         }
  58.         if ($endDay == 31) {
  59.             if ($methodUS && $startDay != 30) {
  60.                 $endDay = 1;
  61.                 if ($endMonth == 12) {
  62.                     ++$endYear;
  63.                     $endMonth = 1;
  64.                 } else {
  65.                     ++$endMonth;
  66.                 }
  67.             } else {
  68.                 $endDay = 30;
  69.             }
  70.         }
  71.  
  72.         return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
  73.     }    //    function _dateDiff360()
  74.  
  75.  
  76.     /**
  77.      * _getDateValue
  78.      *
  79.      * @param    string    $dateValue 
  80.      * @return    mixed    Excel date/time serial value, or string if error
  81.      */
  82.     public static function _getDateValue($dateValue) {
  83.         if (!is_numeric($dateValue)) {
  84.             if ((is_string($dateValue)) && (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) {
  85.                 return PHPExcel_Calculation_Functions::VALUE();
  86.             }
  87.             if ((is_object($dateValue)) && ($dateValue instanceof PHPExcel_Shared_Date::$dateTimeObjectType)) {
  88.                 $dateValue = PHPExcel_Shared_Date::PHPToExcel($dateValue);
  89.             } else {
  90.                 $saveReturnDateType = PHPExcel_Calculation_Functions::getReturnDateType();
  91.                 PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  92.                 $dateValue = self::DATEVALUE($dateValue);
  93.                 PHPExcel_Calculation_Functions::setReturnDateType($saveReturnDateType);
  94.             }
  95.         }
  96.         return $dateValue;
  97.     }    //    function _getDateValue()
  98.  
  99.  
  100.     /**
  101.      * _getTimeValue
  102.      *
  103.      * @param    string    $timeValue 
  104.      * @return    mixed    Excel date/time serial value, or string if error
  105.      */
  106.     private static function _getTimeValue($timeValue) {
  107.         $saveReturnDateType = PHPExcel_Calculation_Functions::getReturnDateType();
  108.         $timeValue = self::TIMEVALUE($timeValue);
  109.         PHPExcel_Calculation_Functions::setReturnDateType($saveReturnDateType);
  110.         return $timeValue;
  111.     }    //    function _getTimeValue()
  112.  
  113.  
  114.     private static function _adjustDateByMonths($dateValue = 0, $adjustmentMonths = 0) {
  115.         // Execute function
  116.         $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  117.         $oMonth = (int) $PHPDateObject->format('m');
  118.         $oYear = (int) $PHPDateObject->format('Y');
  119.  
  120.         $adjustmentMonthsString = (string) $adjustmentMonths;
  121.         if ($adjustmentMonths > 0) {
  122.             $adjustmentMonthsString = '+'.$adjustmentMonths;
  123.         }
  124.         if ($adjustmentMonths != 0) {
  125.             $PHPDateObject->modify($adjustmentMonthsString.' months');
  126.         }
  127.         $nMonth = (int) $PHPDateObject->format('m');
  128.         $nYear = (int) $PHPDateObject->format('Y');
  129.  
  130.         $monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
  131.         if ($monthDiff != $adjustmentMonths) {
  132.             $adjustDays = (int) $PHPDateObject->format('d');
  133.             $adjustDaysString = '-'.$adjustDays.' days';
  134.             $PHPDateObject->modify($adjustDaysString);
  135.         }
  136.         return $PHPDateObject;
  137.     }    //    function _adjustDateByMonths()
  138.  
  139.  
  140.     /**
  141.      * DATETIMENOW
  142.      *
  143.      * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  144.      *                         depending on the value of the ReturnDateType flag
  145.      */
  146.     public static function DATETIMENOW() {
  147.         $saveTimeZone = date_default_timezone_get();
  148.         date_default_timezone_set('UTC');
  149.         $retValue = False;
  150.         switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  151.             case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  152.                     $retValue = (float) PHPExcel_Shared_Date::PHPToExcel(time());
  153.                     break;
  154.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  155.                     $retValue = (integer) time();
  156.                     break;
  157.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  158.                     $retValue = new DateTime();
  159.                     break;
  160.         }
  161.         date_default_timezone_set($saveTimeZone);
  162.  
  163.         return $retValue;
  164.     }    //    function DATETIMENOW()
  165.  
  166.  
  167.     /**
  168.      * DATENOW
  169.      *
  170.      * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  171.      *                         depending on the value of the ReturnDateType flag
  172.      */
  173.     public static function DATENOW() {
  174.         $saveTimeZone = date_default_timezone_get();
  175.         date_default_timezone_set('UTC');
  176.         $retValue = False;
  177.         $excelDateTime = floor(PHPExcel_Shared_Date::PHPToExcel(time()));
  178.         switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  179.             case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  180.                     $retValue = (float) $excelDateTime;
  181.                     break;
  182.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  183.                     $retValue = (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateTime) - 3600;
  184.                     break;
  185.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  186.                     $retValue = PHPExcel_Shared_Date::ExcelToPHPObject($excelDateTime);
  187.                     break;
  188.         }
  189.         date_default_timezone_set($saveTimeZone);
  190.  
  191.         return $retValue;
  192.     }    //    function DATENOW()
  193.  
  194.  
  195.     /**
  196.      * DATE
  197.      *
  198.      * @param    long    $year 
  199.      * @param    long    $month 
  200.      * @param    long    $day 
  201.      * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  202.      *                         depending on the value of the ReturnDateType flag
  203.      */
  204.     public static function DATE($year = 0, $month = 1, $day = 1) {
  205.         $year    = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($year);
  206.         $month    = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($month);
  207.         $day    = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($day);
  208.  
  209.         $baseYear = PHPExcel_Shared_Date::getExcelCalendar();
  210.         // Validate parameters
  211.         if ($year < ($baseYear-1900)) {
  212.             return PHPExcel_Calculation_Functions::NaN();
  213.         }
  214.         if ((($baseYear-1900) != 0) && ($year < $baseYear) && ($year >= 1900)) {
  215.             return PHPExcel_Calculation_Functions::NaN();
  216.         }
  217.  
  218.         if (($year < $baseYear) && ($year >= ($baseYear-1900))) {
  219.             $year += 1900;
  220.         }
  221.  
  222.         if ($month < 1) {
  223.             //    Handle year/month adjustment if month < 1
  224.             --$month;
  225.             $year += ceil($month / 12) - 1;
  226.             $month = 13 - abs($month % 12);
  227.         } elseif ($month > 12) {
  228.             //    Handle year/month adjustment if month > 12
  229.             $year += floor($month / 12);
  230.             $month = ($month % 12);
  231.         }
  232.  
  233.         // Re-validate the year parameter after adjustments
  234.         if (($year < $baseYear) || ($year >= 10000)) {
  235.             return PHPExcel_Calculation_Functions::NaN();
  236.         }
  237.  
  238.         // Execute function
  239.         $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel($year, $month, $day);
  240.         switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  241.             case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  242.                     return (float) $excelDateValue;
  243.                     break;
  244.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  245.                     return (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateValue);
  246.                     break;
  247.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  248.                     return PHPExcel_Shared_Date::ExcelToPHPObject($excelDateValue);
  249.                     break;
  250.         }
  251.     }    //    function DATE()
  252.  
  253.  
  254.     /**
  255.      * TIME
  256.      *
  257.      * @param    long    $hour 
  258.      * @param    long    $minute 
  259.      * @param    long    $second 
  260.      * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  261.      *                         depending on the value of the ReturnDateType flag
  262.      */
  263.     public static function TIME($hour = 0, $minute = 0, $second = 0) {
  264.         $hour    = PHPExcel_Calculation_Functions::flattenSingleValue($hour);
  265.         $minute    = PHPExcel_Calculation_Functions::flattenSingleValue($minute);
  266.         $second    = PHPExcel_Calculation_Functions::flattenSingleValue($second);
  267.  
  268.         if ($hour == '') { $hour = 0; }
  269.         if ($minute == '') { $minute = 0; }
  270.         if ($second == '') { $second = 0; }
  271.  
  272.         if ((!is_numeric($hour)) || (!is_numeric($minute)) || (!is_numeric($second))) {
  273.             return PHPExcel_Calculation_Functions::VALUE();
  274.         }
  275.         $hour    = (integer) $hour;
  276.         $minute    = (integer) $minute;
  277.         $second    = (integer) $second;
  278.  
  279.         if ($second < 0) {
  280.             $minute += floor($second / 60);
  281.             $second = 60 - abs($second % 60);
  282.             if ($second == 60) { $second = 0; }
  283.         } elseif ($second >= 60) {
  284.             $minute += floor($second / 60);
  285.             $second = $second % 60;
  286.         }
  287.         if ($minute < 0) {
  288.             $hour += floor($minute / 60);
  289.             $minute = 60 - abs($minute % 60);
  290.             if ($minute == 60) { $minute = 0; }
  291.         } elseif ($minute >= 60) {
  292.             $hour += floor($minute / 60);
  293.             $minute = $minute % 60;
  294.         }
  295.  
  296.         if ($hour > 23) {
  297.             $hour = $hour % 24;
  298.         } elseif ($hour < 0) {
  299.             return PHPExcel_Calculation_Functions::NaN();
  300.         }
  301.  
  302.         // Execute function
  303.         switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  304.             case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  305.                     $date = 0;
  306.                     $calendar = PHPExcel_Shared_Date::getExcelCalendar();
  307.                     if ($calendar != PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900) {
  308.                         $date = 1;
  309.                     }
  310.                     return (float) PHPExcel_Shared_Date::FormattedPHPToExcel($calendar, 1, $date, $hour, $minute, $second);
  311.                     break;
  312.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  313.                     return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::FormattedPHPToExcel(1970, 1, 1, $hour-1, $minute, $second));    // -2147468400; //    -2147472000 + 3600
  314.                     break;
  315.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  316.                     $dayAdjust = 0;
  317.                     if ($hour < 0) {
  318.                         $dayAdjust = floor($hour / 24);
  319.                         $hour = 24 - abs($hour % 24);
  320.                         if ($hour == 24) { $hour = 0; }
  321.                     } elseif ($hour >= 24) {
  322.                         $dayAdjust = floor($hour / 24);
  323.                         $hour = $hour % 24;
  324.                     }
  325.                     $phpDateObject = new DateTime('1900-01-01 '.$hour.':'.$minute.':'.$second);
  326.                     if ($dayAdjust != 0) {
  327.                         $phpDateObject->modify($dayAdjust.' days');
  328.                     }
  329.                     return $phpDateObject;
  330.                     break;
  331.         }
  332.     }    //    function TIME()
  333.  
  334.  
  335.     /**
  336.      * DATEVALUE
  337.      *
  338.      * @param    string    $dateValue 
  339.      * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  340.      *                         depending on the value of the ReturnDateType flag
  341.      */
  342.     public static function DATEVALUE($dateValue = 1) {
  343.         $dateValue = trim(PHPExcel_Calculation_Functions::flattenSingleValue($dateValue),'"');
  344.         //    Strip any ordinals because they're allowed in Excel (English only)
  345.         $dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui','$1$3',$dateValue);
  346.         //    Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany)
  347.         $dateValue    = str_replace(array('/','.','-','  '),array(' ',' ',' ',' '),$dateValue);
  348.  
  349.         $yearFound = false;
  350.         $t1 = explode(' ',$dateValue);
  351.         foreach($t1 as &$t) {
  352.             if ((is_numeric($t)) && ($t > 31)) {
  353.                 if ($yearFound) {
  354.                     return PHPExcel_Calculation_Functions::VALUE();
  355.                 } else {
  356.                     if ($t < 100) { $t += 1900; }
  357.                     $yearFound = true;
  358.                 }
  359.             }
  360.         }
  361.         if ((count($t1) == 1) && (strpos($t,':') != false)) {
  362.             //    We've been fed a time value without any date
  363.             return 0.0;
  364.         } elseif (count($t1) == 2) {
  365.             //    We only have two parts of the date: either day/month or month/year
  366.             if ($yearFound) {
  367.                 array_unshift($t1,1);
  368.             } else {
  369.                 array_push($t1,date('Y'));
  370.             }
  371.         }
  372.         unset($t);
  373.         $dateValue = implode(' ',$t1);
  374.  
  375.         $PHPDateArray = date_parse($dateValue);
  376.         if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
  377.             $testVal1 = strtok($dateValue,'- ');
  378.             if ($testVal1 !== False) {
  379.                 $testVal2 = strtok('- ');
  380.                 if ($testVal2 !== False) {
  381.                     $testVal3 = strtok('- ');
  382.                     if ($testVal3 === False) {
  383.                         $testVal3 = strftime('%Y');
  384.                     }
  385.                 } else {
  386.                     return PHPExcel_Calculation_Functions::VALUE();
  387.                 }
  388.             } else {
  389.                 return PHPExcel_Calculation_Functions::VALUE();
  390.             }
  391.             $PHPDateArray = date_parse($testVal1.'-'.$testVal2.'-'.$testVal3);
  392.             if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
  393.                 $PHPDateArray = date_parse($testVal2.'-'.$testVal1.'-'.$testVal3);
  394.                 if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
  395.                     return PHPExcel_Calculation_Functions::VALUE();
  396.                 }
  397.             }
  398.         }
  399.  
  400.         if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
  401.             // Execute function
  402.             if ($PHPDateArray['year'] == '')    { $PHPDateArray['year'] = strftime('%Y'); }
  403.             if ($PHPDateArray['month'] == '')    { $PHPDateArray['month'] = strftime('%m'); }
  404.             if ($PHPDateArray['day'] == '')        { $PHPDateArray['day'] = strftime('%d'); }
  405.             $excelDateValue = floor(PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray['year'],$PHPDateArray['month'],$PHPDateArray['day'],$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']));
  406.  
  407.             switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  408.                 case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  409.                         return (float) $excelDateValue;
  410.                         break;
  411.                 case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  412.                         return (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateValue);
  413.                         break;
  414.                 case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  415.                         return new DateTime($PHPDateArray['year'].'-'.$PHPDateArray['month'].'-'.$PHPDateArray['day'].' 00:00:00');
  416.                         break;
  417.             }
  418.         }
  419.         return PHPExcel_Calculation_Functions::VALUE();
  420.     }    //    function DATEVALUE()
  421.  
  422.  
  423.     /**
  424.      * TIMEVALUE
  425.      *
  426.      * @param    string    $timeValue 
  427.      * @return    mixed    Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  428.      *                         depending on the value of the ReturnDateType flag
  429.      */
  430.     public static function TIMEVALUE($timeValue) {
  431.         $timeValue = trim(PHPExcel_Calculation_Functions::flattenSingleValue($timeValue),'"');
  432.         $timeValue    = str_replace(array('/','.'),array('-','-'),$timeValue);
  433.  
  434.         $PHPDateArray = date_parse($timeValue);
  435.         if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
  436.             if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  437.                 $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray['year'],$PHPDateArray['month'],$PHPDateArray['day'],$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']);
  438.             } else {
  439.                 $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel(1900,1,1,$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']) - 1;
  440.             }
  441.  
  442.             switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  443.                 case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  444.                         return (float) $excelDateValue;
  445.                         break;
  446.                 case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  447.                         return (integer) $phpDateValue = PHPExcel_Shared_Date::ExcelToPHP($excelDateValue+25569) - 3600;;
  448.                         break;
  449.                 case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  450.                         return new DateTime('1900-01-01 '.$PHPDateArray['hour'].':'.$PHPDateArray['minute'].':'.$PHPDateArray['second']);
  451.                         break;
  452.             }
  453.         }
  454.         return PHPExcel_Calculation_Functions::VALUE();
  455.     }    //    function TIMEVALUE()
  456.  
  457.  
  458.     /**
  459.      * DATEDIF
  460.      *
  461.      * @param    long    $startDate        Excel date serial value or a standard date string
  462.      * @param    long    $endDate        Excel date serial value or a standard date string
  463.      * @param    string    $unit 
  464.      * @return    long    Interval between the dates
  465.      */
  466.     public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D') {
  467.         $startDate    = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
  468.         $endDate    = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
  469.         $unit        = strtoupper(PHPExcel_Calculation_Functions::flattenSingleValue($unit));
  470.  
  471.         if (is_string($startDate = self::_getDateValue($startDate))) {
  472.             return PHPExcel_Calculation_Functions::VALUE();
  473.         }
  474.         if (is_string($endDate = self::_getDateValue($endDate))) {
  475.             return PHPExcel_Calculation_Functions::VALUE();
  476.         }
  477.  
  478.         // Validate parameters
  479.         if ($startDate >= $endDate) {
  480.             return PHPExcel_Calculation_Functions::NaN();
  481.         }
  482.  
  483.         // Execute function
  484.         $difference = $endDate - $startDate;
  485.  
  486.         $PHPStartDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($startDate);
  487.         $startDays = $PHPStartDateObject->format('j');
  488.         $startMonths = $PHPStartDateObject->format('n');
  489.         $startYears = $PHPStartDateObject->format('Y');
  490.  
  491.         $PHPEndDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
  492.         $endDays = $PHPEndDateObject->format('j');
  493.         $endMonths = $PHPEndDateObject->format('n');
  494.         $endYears = $PHPEndDateObject->format('Y');
  495.  
  496.         $retVal = PHPExcel_Calculation_Functions::NaN();
  497.         switch ($unit) {
  498.             case 'D':
  499.                 $retVal = intval($difference);
  500.                 break;
  501.             case 'M':
  502.                 $retVal = intval($endMonths - $startMonths) + (intval($endYears - $startYears) * 12);
  503.                 //    We're only interested in full months
  504.                 if ($endDays < $startDays) {
  505.                     --$retVal;
  506.                 }
  507.                 break;
  508.             case 'Y':
  509.                 $retVal = intval($endYears - $startYears);
  510.                 //    We're only interested in full months
  511.                 if ($endMonths < $startMonths) {
  512.                     --$retVal;
  513.                 } elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
  514.                     --$retVal;
  515.                 }
  516.                 break;
  517.             case 'MD':
  518.                 if ($endDays < $startDays) {
  519.                     $retVal = $endDays;
  520.                     $PHPEndDateObject->modify('-'.$endDays.' days');
  521.                     $adjustDays = $PHPEndDateObject->format('j');
  522.                     if ($adjustDays > $startDays) {
  523.                         $retVal += ($adjustDays - $startDays);
  524.                     }
  525.                 } else {
  526.                     $retVal = $endDays - $startDays;
  527.                 }
  528.                 break;
  529.             case 'YM':
  530.                 $retVal = intval($endMonths - $startMonths);
  531.                 if ($retVal < 0) $retVal = 12 + $retVal;
  532.                 //    We're only interested in full months
  533.                 if ($endDays < $startDays) {
  534.                     --$retVal;
  535.                 }
  536.                 break;
  537.             case 'YD':
  538.                 $retVal = intval($difference);
  539.                 if ($endYears > $startYears) {
  540.                     while ($endYears > $startYears) {
  541.                         $PHPEndDateObject->modify('-1 year');
  542.                         $endYears = $PHPEndDateObject->format('Y');
  543.                     }
  544.                     $retVal = $PHPEndDateObject->format('z') - $PHPStartDateObject->format('z');
  545.                     if ($retVal < 0) { $retVal += 365; }
  546.                 }
  547.                 break;
  548.         }
  549.         return $retVal;
  550.     }    //    function DATEDIF()
  551.  
  552.  
  553.     /**
  554.      * DAYS360
  555.      *
  556.      * @param    long    $startDate        Excel date serial value or a standard date string
  557.      * @param    long    $endDate        Excel date serial value or a standard date string
  558.      * @param    boolean    $method            US or European Method
  559.      * @return    long    PHP date/time serial
  560.      */
  561.     public static function DAYS360($startDate = 0, $endDate = 0, $method = false) {
  562.         $startDate    = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
  563.         $endDate    = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
  564.  
  565.         if (is_string($startDate = self::_getDateValue($startDate))) {
  566.             return PHPExcel_Calculation_Functions::VALUE();
  567.         }
  568.         if (is_string($endDate = self::_getDateValue($endDate))) {
  569.             return PHPExcel_Calculation_Functions::VALUE();
  570.         }
  571.  
  572.         // Execute function
  573.         $PHPStartDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($startDate);
  574.         $startDay = $PHPStartDateObject->format('j');
  575.         $startMonth = $PHPStartDateObject->format('n');
  576.         $startYear = $PHPStartDateObject->format('Y');
  577.  
  578.         $PHPEndDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
  579.         $endDay = $PHPEndDateObject->format('j');
  580.         $endMonth = $PHPEndDateObject->format('n');
  581.         $endYear = $PHPEndDateObject->format('Y');
  582.  
  583.         return self::_dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, !$method);
  584.     }    //    function DAYS360()
  585.  
  586.  
  587.     /**
  588.      *    YEARFRAC
  589.      *
  590.      *    Calculates the fraction of the year represented by the number of whole days between two dates (the start_date and the
  591.      *    end_date). Use the YEARFRAC worksheet function to identify the proportion of a whole year's benefits or obligations
  592.      *    to assign to a specific term.
  593.      *
  594.      *    @param    mixed    $startDate        Excel date serial value (float), PHP date timestamp (integer) or date object, or a standard date string
  595.      *    @param    mixed    $endDate        Excel date serial value (float), PHP date timestamp (integer) or date object, or a standard date string
  596.      *    @param    integer    $method            Method used for the calculation
  597.      *                                         0 or omitted    US (NASD) 30/360
  598.      *                                         1                Actual/actual
  599.      *                                         2                Actual/360
  600.      *                                         3                Actual/365
  601.      *                                         4                European 30/360
  602.      *    @return    float    fraction of the year
  603.      */
  604.     public static function YEARFRAC($startDate = 0, $endDate = 0, $method = 0) {
  605.         $startDate    = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
  606.         $endDate    = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
  607.         $method        = PHPExcel_Calculation_Functions::flattenSingleValue($method);
  608.  
  609.         if (is_string($startDate = self::_getDateValue($startDate))) {
  610.             return PHPExcel_Calculation_Functions::VALUE();
  611.         }
  612.         if (is_string($endDate = self::_getDateValue($endDate))) {
  613.             return PHPExcel_Calculation_Functions::VALUE();
  614.         }
  615.  
  616.         if (((is_numeric($method)) && (!is_string($method))) || ($method == '')) {
  617.             switch($method) {
  618.                 case 0    :
  619.                     return self::DAYS360($startDate,$endDate) / 360;
  620.                     break;
  621.                 case 1    :
  622.                     $days = self::DATEDIF($startDate,$endDate);
  623.                     $startYear = self::YEAR($startDate);
  624.                     $endYear = self::YEAR($endDate);
  625.                     $years = $endYear - $startYear + 1;
  626.                     $leapDays = 0;
  627.                     if ($years == 1) {
  628.                         if (self::_isLeapYear($endYear)) {
  629.                             $startMonth = self::MONTHOFYEAR($startDate);
  630.                             $endMonth = self::MONTHOFYEAR($endDate);
  631.                             $endDay = self::DAYOFMONTH($endDate);
  632.                             if (($startMonth < 3) ||
  633.                                 (($endMonth * 100 + $endDay) >= (2 * 100 + 29))) {
  634.                                  $leapDays += 1;
  635.                             }
  636.                         }
  637.                     } else {
  638.                         for($year = $startYear; $year <= $endYear; ++$year) {
  639.                             if ($year == $startYear) {
  640.                                 $startMonth = self::MONTHOFYEAR($startDate);
  641.                                 $startDay = self::DAYOFMONTH($startDate);
  642.                                 if ($startMonth < 3) {
  643.                                     $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
  644.                                 }
  645.                             } elseif($year == $endYear) {
  646.                                 $endMonth = self::MONTHOFYEAR($endDate);
  647.                                 $endDay = self::DAYOFMONTH($endDate);
  648.                                 if (($endMonth * 100 + $endDay) >= (2 * 100 + 29)) {
  649.                                     $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
  650.                                 }
  651.                             } else {
  652.                                 $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
  653.                             }
  654.                         }
  655.                         if ($years == 2) {
  656.                             if (($leapDays == 0) && (self::_isLeapYear($startYear)) && ($days > 365)) {
  657.                                 $leapDays = 1;
  658.                             } elseif ($days < 366) {
  659.                                 $years = 1;
  660.                             }
  661.                         }
  662.                         $leapDays /= $years;
  663.                     }
  664.                     return $days / (365 + $leapDays);
  665.                     break;
  666.                 case 2    :
  667.                     return self::DATEDIF($startDate,$endDate) / 360;
  668.                     break;
  669.                 case 3    :
  670.                     return self::DATEDIF($startDate,$endDate) / 365;
  671.                     break;
  672.                 case 4    :
  673.                     return self::DAYS360($startDate,$endDate,True) / 360;
  674.                     break;
  675.             }
  676.         }
  677.         return PHPExcel_Calculation_Functions::VALUE();
  678.     }    //    function YEARFRAC()
  679.  
  680.  
  681.     /**
  682.      * NETWORKDAYS
  683.      *
  684.      * @param    mixed                Start date
  685.      * @param    mixed                End date
  686.      * @param    array of mixed        Optional Date Series
  687.      * @return    long    Interval between the dates
  688.      */
  689.     public static function NETWORKDAYS($startDate,$endDate) {
  690.         //    Retrieve the mandatory start and end date that are referenced in the function definition
  691.         $startDate    = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
  692.         $endDate    = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
  693.         //    Flush the mandatory start and end date that are referenced in the function definition, and get the optional days
  694.         $dateArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  695.         array_shift($dateArgs);
  696.         array_shift($dateArgs);
  697.  
  698.         //    Validate the start and end dates
  699.         if (is_string($startDate = $sDate = self::_getDateValue($startDate))) {
  700.             return PHPExcel_Calculation_Functions::VALUE();
  701.         }
  702.         $startDate = (float) floor($startDate);
  703.         if (is_string($endDate = $eDate = self::_getDateValue($endDate))) {
  704.             return PHPExcel_Calculation_Functions::VALUE();
  705.         }
  706.         $endDate = (float) floor($endDate);
  707.  
  708.         if ($sDate > $eDate) {
  709.             $startDate = $eDate;
  710.             $endDate = $sDate;
  711.         }
  712.  
  713.         // Execute function
  714.         $startDoW = 6 - self::DAYOFWEEK($startDate,2);
  715.         if ($startDoW < 0) { $startDoW = 0; }
  716.         $endDoW = self::DAYOFWEEK($endDate,2);
  717.         if ($endDoW >= 6) { $endDoW = 0; }
  718.  
  719.         $wholeWeekDays = floor(($endDate - $startDate) / 7) * 5;
  720.         $partWeekDays = $endDoW + $startDoW;
  721.         if ($partWeekDays > 5) {
  722.             $partWeekDays -= 5;
  723.         }
  724.  
  725.         //    Test any extra holiday parameters
  726.         $holidayCountedArray = array();
  727.         foreach ($dateArgs as $holidayDate) {
  728.             if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
  729.                 return PHPExcel_Calculation_Functions::VALUE();
  730.             }
  731.             if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
  732.                 if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
  733.                     --$partWeekDays;
  734.                     $holidayCountedArray[] = $holidayDate;
  735.                 }
  736.             }
  737.         }
  738.  
  739.         if ($sDate > $eDate) {
  740.             return 0 - ($wholeWeekDays + $partWeekDays);
  741.         }
  742.         return $wholeWeekDays + $partWeekDays;
  743.     }    //    function NETWORKDAYS()
  744.  
  745.  
  746.     /**
  747.      * WORKDAY
  748.      *
  749.      * @param    mixed                Start date
  750.      * @param    mixed                number of days for adjustment
  751.      * @param    array of mixed        Optional Date Series
  752.      * @return    long    Interval between the dates
  753.      */
  754.     public static function WORKDAY($startDate,$endDays) {
  755.         //    Retrieve the mandatory start date and days that are referenced in the function definition
  756.         $startDate    = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
  757.         $endDays    = (int) PHPExcel_Calculation_Functions::flattenSingleValue($endDays);
  758.         //    Flush the mandatory start date and days that are referenced in the function definition, and get the optional days
  759.         $dateArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  760.         array_shift($dateArgs);
  761.         array_shift($dateArgs);
  762.  
  763.         if ((is_string($startDate = self::_getDateValue($startDate))) || (!is_numeric($endDays))) {
  764.             return PHPExcel_Calculation_Functions::VALUE();
  765.         }
  766.         $startDate = (float) floor($startDate);
  767.         //    If endDays is 0, we always return startDate
  768.         if ($endDays == 0) { return $startDate; }
  769.  
  770.         $decrementing = ($endDays < 0) ? True : False;
  771.  
  772.         //    Adjust the start date if it falls over a weekend
  773.  
  774.         $startDoW = self::DAYOFWEEK($startDate,3);
  775.         if (self::DAYOFWEEK($startDate,3) >= 5) {
  776.             $startDate += ($decrementing) ? -$startDoW + 4: 7 - $startDoW;
  777.             ($decrementing) ? $endDays++ : $endDays--;
  778.         }
  779.  
  780.         //    Add endDays
  781.         $endDate = (float) $startDate + (intval($endDays / 5) * 7) + ($endDays % 5);
  782.  
  783.         //    Adjust the calculated end date if it falls over a weekend
  784.         $endDoW = self::DAYOFWEEK($endDate,3);
  785.         if ($endDoW >= 5) {
  786.             $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
  787.         }
  788.  
  789.         //    Test any extra holiday parameters
  790.         if (count($dateArgs) > 0) {
  791.             $holidayCountedArray = $holidayDates = array();
  792.             foreach ($dateArgs as $holidayDate) {
  793.                 if ((!is_null($holidayDate)) && (trim($holidayDate) > '')) {
  794.                     if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
  795.                         return PHPExcel_Calculation_Functions::VALUE();
  796.                     }
  797.                     if (self::DAYOFWEEK($holidayDate,3) < 5) {
  798.                         $holidayDates[] = $holidayDate;
  799.                     }
  800.                 }
  801.             }
  802.             if ($decrementing) {
  803.                 rsort($holidayDates, SORT_NUMERIC);
  804.             } else {
  805.                 sort($holidayDates, SORT_NUMERIC);
  806.             }
  807.             foreach ($holidayDates as $holidayDate) {
  808.                 if ($decrementing) {
  809.                     if (($holidayDate <= $startDate) && ($holidayDate >= $endDate)) {
  810.                         if (!in_array($holidayDate,$holidayCountedArray)) {
  811.                             --$endDate;
  812.                             $holidayCountedArray[] = $holidayDate;
  813.                         }
  814.                     }
  815.                 } else {
  816.                     if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
  817.                         if (!in_array($holidayDate,$holidayCountedArray)) {
  818.                             ++$endDate;
  819.                             $holidayCountedArray[] = $holidayDate;
  820.                         }
  821.                     }
  822.                 }
  823.                 //    Adjust the calculated end date if it falls over a weekend
  824.                 $endDoW = self::DAYOFWEEK($endDate,3);
  825.                 if ($endDoW >= 5) {
  826.                     $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
  827.                 }
  828.  
  829.             }
  830.         }
  831.  
  832.         switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  833.             case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  834.                     return (float) $endDate;
  835.                     break;
  836.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  837.                     return (integer) PHPExcel_Shared_Date::ExcelToPHP($endDate);
  838.                     break;
  839.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  840.                     return PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
  841.                     break;
  842.         }
  843.     }    //    function WORKDAY()
  844.  
  845.  
  846.     /**
  847.      * DAYOFMONTH
  848.      *
  849.      * @param    long    $dateValue        Excel date serial value or a standard date string
  850.      * @return    int        Day
  851.      */
  852.     public static function DAYOFMONTH($dateValue = 1) {
  853.         $dateValue    = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  854.  
  855.         if (is_string($dateValue = self::_getDateValue($dateValue))) {
  856.             return PHPExcel_Calculation_Functions::VALUE();
  857.         } elseif ($dateValue == 0.0) {
  858.             return 0;
  859.         } elseif ($dateValue < 0.0) {
  860.             return PHPExcel_Calculation_Functions::NaN();
  861.         }
  862.  
  863.         // Execute function
  864.         $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  865.  
  866.         return (int) $PHPDateObject->format('j');
  867.     }    //    function DAYOFMONTH()
  868.  
  869.  
  870.     /**
  871.      * DAYOFWEEK
  872.      *
  873.      * @param    long    $dateValue        Excel date serial value or a standard date string
  874.      * @return    int        Day
  875.      */
  876.     public static function DAYOFWEEK($dateValue = 1, $style = 1) {
  877.         $dateValue    = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  878.         $style        = floor(PHPExcel_Calculation_Functions::flattenSingleValue($style));
  879.  
  880.         if (is_string($dateValue = self::_getDateValue($dateValue))) {
  881.             return PHPExcel_Calculation_Functions::VALUE();
  882.         } elseif ($dateValue < 0.0) {
  883.             return PHPExcel_Calculation_Functions::NaN();
  884.         }
  885.  
  886.         // Execute function
  887.         $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  888.         $DoW = $PHPDateObject->format('w');
  889.  
  890.         $firstDay = 1;
  891.         switch ($style) {
  892.             case 1: ++$DoW;
  893.                     break;
  894.             case 2: if ($DoW == 0) { $DoW = 7; }
  895.                     break;
  896.             case 3: if ($DoW == 0) { $DoW = 7; }
  897.                     $firstDay = 0;
  898.                     --$DoW;
  899.                     break;
  900.             default:
  901.         }
  902.         if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL) {
  903.             //    Test for Excel's 1900 leap year, and introduce the error as required
  904.             if (($PHPDateObject->format('Y') == 1900) && ($PHPDateObject->format('n') <= 2)) {
  905.                 --$DoW;
  906.                 if ($DoW < $firstDay) {
  907.                     $DoW += 7;
  908.                 }
  909.             }
  910.         }
  911.  
  912.         return (int) $DoW;
  913.     }    //    function DAYOFWEEK()
  914.  
  915.  
  916.     /**
  917.      * WEEKOFYEAR
  918.      *
  919.      * @param    long    $dateValue        Excel date serial value or a standard date string
  920.      * @param    boolean    $method            Week begins on Sunday or Monday
  921.      * @return    int        Week Number
  922.      */
  923.     public static function WEEKOFYEAR($dateValue = 1, $method = 1) {
  924.         $dateValue    = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  925.         $method        = floor(PHPExcel_Calculation_Functions::flattenSingleValue($method));
  926.  
  927.         if (!is_numeric($method)) {
  928.             return PHPExcel_Calculation_Functions::VALUE();
  929.         } elseif (($method < 1) || ($method > 2)) {
  930.             return PHPExcel_Calculation_Functions::NaN();
  931.         }
  932.  
  933.         if (is_string($dateValue = self::_getDateValue($dateValue))) {
  934.             return PHPExcel_Calculation_Functions::VALUE();
  935.         } elseif ($dateValue < 0.0) {
  936.             return PHPExcel_Calculation_Functions::NaN();
  937.         }
  938.  
  939.         // Execute function
  940.         $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  941.         $dayOfYear = $PHPDateObject->format('z');
  942.         $dow = $PHPDateObject->format('w');
  943.         $PHPDateObject->modify('-'.$dayOfYear.' days');
  944.         $dow = $PHPDateObject->format('w');
  945.         $daysInFirstWeek = 7 - (($dow + (2 - $method)) % 7);
  946.         $dayOfYear -= $daysInFirstWeek;
  947.         $weekOfYear = ceil($dayOfYear / 7) + 1;
  948.  
  949.         return (int) $weekOfYear;
  950.     }    //    function WEEKOFYEAR()
  951.  
  952.  
  953.     /**
  954.      * MONTHOFYEAR
  955.      *
  956.      * @param    long    $dateValue        Excel date serial value or a standard date string
  957.      * @return    int        Month
  958.      */
  959.     public static function MONTHOFYEAR($dateValue = 1) {
  960.         $dateValue    = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  961.  
  962.         if (is_string($dateValue = self::_getDateValue($dateValue))) {
  963.             return PHPExcel_Calculation_Functions::VALUE();
  964.         } elseif ($dateValue < 0.0) {
  965.             return PHPExcel_Calculation_Functions::NaN();
  966.         }
  967.  
  968.         // Execute function
  969.         $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  970.  
  971.         return (int) $PHPDateObject->format('n');
  972.     }    //    function MONTHOFYEAR()
  973.  
  974.  
  975.     /**
  976.      * YEAR
  977.      *
  978.      * @param    long    $dateValue        Excel date serial value or a standard date string
  979.      * @return    int        Year
  980.      */
  981.     public static function YEAR($dateValue = 1) {
  982.         $dateValue    = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  983.  
  984.         if (is_string($dateValue = self::_getDateValue($dateValue))) {
  985.             return PHPExcel_Calculation_Functions::VALUE();
  986.         } elseif ($dateValue < 0.0) {
  987.             return PHPExcel_Calculation_Functions::NaN();
  988.         }
  989.  
  990.         // Execute function
  991.         $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  992.  
  993.         return (int) $PHPDateObject->format('Y');
  994.     }    //    function YEAR()
  995.  
  996.  
  997.     /**
  998.      * HOUROFDAY
  999.      *
  1000.      * @param    mixed    $timeValue        Excel time serial value or a standard time string
  1001.      * @return    int        Hour
  1002.      */
  1003.     public static function HOUROFDAY($timeValue = 0) {
  1004.         $timeValue    = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
  1005.  
  1006.         if (!is_numeric($timeValue)) {
  1007.             if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1008.                 $testVal = strtok($timeValue,'/-: ');
  1009.                 if (strlen($testVal) < strlen($timeValue)) {
  1010.                     return PHPExcel_Calculation_Functions::VALUE();
  1011.                 }
  1012.             }
  1013.             $timeValue = self::_getTimeValue($timeValue);
  1014.             if (is_string($timeValue)) {
  1015.                 return PHPExcel_Calculation_Functions::VALUE();
  1016.             }
  1017.         }
  1018.         // Execute function
  1019.         if ($timeValue >= 1) {
  1020.             $timeValue = fmod($timeValue,1);
  1021.         } elseif ($timeValue < 0.0) {
  1022.             return PHPExcel_Calculation_Functions::NaN();
  1023.         }
  1024.         $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
  1025.  
  1026.         return (int) gmdate('G',$timeValue);
  1027.     }    //    function HOUROFDAY()
  1028.  
  1029.  
  1030.     /**
  1031.      * MINUTEOFHOUR
  1032.      *
  1033.      * @param    long    $timeValue        Excel time serial value or a standard time string
  1034.      * @return    int        Minute
  1035.      */
  1036.     public static function MINUTEOFHOUR($timeValue = 0) {
  1037.         $timeValue = $timeTester    = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
  1038.  
  1039.         if (!is_numeric($timeValue)) {
  1040.             if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1041.                 $testVal = strtok($timeValue,'/-: ');
  1042.                 if (strlen($testVal) < strlen($timeValue)) {
  1043.                     return PHPExcel_Calculation_Functions::VALUE();
  1044.                 }
  1045.             }
  1046.             $timeValue = self::_getTimeValue($timeValue);
  1047.             if (is_string($timeValue)) {
  1048.                 return PHPExcel_Calculation_Functions::VALUE();
  1049.             }
  1050.         }
  1051.         // Execute function
  1052.         if ($timeValue >= 1) {
  1053.             $timeValue = fmod($timeValue,1);
  1054.         } elseif ($timeValue < 0.0) {
  1055.             return PHPExcel_Calculation_Functions::NaN();
  1056.         }
  1057.         $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
  1058.  
  1059.         return (int) gmdate('i',$timeValue);
  1060.     }    //    function MINUTEOFHOUR()
  1061.  
  1062.  
  1063.     /**
  1064.      * SECONDOFMINUTE
  1065.      *
  1066.      * @param    long    $timeValue        Excel time serial value or a standard time string
  1067.      * @return    int        Second
  1068.      */
  1069.     public static function SECONDOFMINUTE($timeValue = 0) {
  1070.         $timeValue    = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
  1071.  
  1072.         if (!is_numeric($timeValue)) {
  1073.             if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1074.                 $testVal = strtok($timeValue,'/-: ');
  1075.                 if (strlen($testVal) < strlen($timeValue)) {
  1076.                     return PHPExcel_Calculation_Functions::VALUE();
  1077.                 }
  1078.             }
  1079.             $timeValue = self::_getTimeValue($timeValue);
  1080.             if (is_string($timeValue)) {
  1081.                 return PHPExcel_Calculation_Functions::VALUE();
  1082.             }
  1083.         }
  1084.         // Execute function
  1085.         if ($timeValue >= 1) {
  1086.             $timeValue = fmod($timeValue,1);
  1087.         } elseif ($timeValue < 0.0) {
  1088.             return PHPExcel_Calculation_Functions::NaN();
  1089.         }
  1090.         $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
  1091.  
  1092.         return (int) gmdate('s',$timeValue);
  1093.     }    //    function SECONDOFMINUTE()
  1094.  
  1095.  
  1096.     /**
  1097.      * EDATE
  1098.      *
  1099.      * Returns the serial number that represents the date that is the indicated number of months before or after a specified date
  1100.      * (the start_date). Use EDATE to calculate maturity dates or due dates that fall on the same day of the month as the date of issue.
  1101.      *
  1102.      * @param    long    $dateValue                Excel date serial value or a standard date string
  1103.      * @param    int        $adjustmentMonths        Number of months to adjust by
  1104.      * @return    long    Excel date serial value
  1105.      */
  1106.     public static function EDATE($dateValue = 1, $adjustmentMonths = 0) {
  1107.         $dateValue            = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1108.         $adjustmentMonths    = floor(PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths));
  1109.  
  1110.         if (!is_numeric($adjustmentMonths)) {
  1111.             return PHPExcel_Calculation_Functions::VALUE();
  1112.         }
  1113.  
  1114.         if (is_string($dateValue = self::_getDateValue($dateValue))) {
  1115.             return PHPExcel_Calculation_Functions::VALUE();
  1116.         }
  1117.  
  1118.         // Execute function
  1119.         $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths);
  1120.  
  1121.         switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  1122.             case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  1123.                     return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
  1124.                     break;
  1125.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  1126.                     return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
  1127.                     break;
  1128.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  1129.                     return $PHPDateObject;
  1130.                     break;
  1131.         }
  1132.     }    //    function EDATE()
  1133.  
  1134.  
  1135.     /**
  1136.      * EOMONTH
  1137.      *
  1138.      * Returns the serial number for the last day of the month that is the indicated number of months before or after start_date.
  1139.      * Use EOMONTH to calculate maturity dates or due dates that fall on the last day of the month.
  1140.      *
  1141.      * @param    long    $dateValue            Excel date serial value or a standard date string
  1142.      * @param    int        $adjustmentMonths    Number of months to adjust by
  1143.      * @return    long    Excel date serial value
  1144.      */
  1145.     public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) {
  1146.         $dateValue            = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1147.         $adjustmentMonths    = floor(PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths));
  1148.  
  1149.         if (!is_numeric($adjustmentMonths)) {
  1150.             return PHPExcel_Calculation_Functions::VALUE();
  1151.         }
  1152.  
  1153.         if (is_string($dateValue = self::_getDateValue($dateValue))) {
  1154.             return PHPExcel_Calculation_Functions::VALUE();
  1155.         }
  1156.  
  1157.         // Execute function
  1158.         $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths+1);
  1159.         $adjustDays = (int) $PHPDateObject->format('d');
  1160.         $adjustDaysString = '-'.$adjustDays.' days';
  1161.         $PHPDateObject->modify($adjustDaysString);
  1162.  
  1163.         switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  1164.             case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  1165.                     return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
  1166.                     break;
  1167.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  1168.                     return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
  1169.                     break;
  1170.             case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  1171.                     return $PHPDateObject;
  1172.                     break;
  1173.         }
  1174.     }    //    function EOMONTH()
  1175.  
  1176. }    //    class PHPExcel_Calculation_DateTime

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