12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178 |
- <?php
- /**
- * PHPExcel
- *
- * Copyright (c) 2006 - 2011 PHPExcel
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * @category PHPExcel
- * @package PHPExcel_Calculation
- * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
- * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
- * @version 1.7.6, 2011-02-27
- */
- /** PHPExcel root directory */
- if (!defined('PHPEXCEL_ROOT')) {
- /**
- * @ignore
- */
- define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
- require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
- }
- /**
- * PHPExcel_Calculation_DateTime
- *
- * @category PHPExcel
- * @package PHPExcel_Calculation
- * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
- */
- class PHPExcel_Calculation_DateTime {
- public static function _isLeapYear($year) {
- return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
- } // function _isLeapYear()
- private static function _dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS) {
- if ($startDay == 31) {
- --$startDay;
- } elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::_isLeapYear($startYear))))) {
- $startDay = 30;
- }
- if ($endDay == 31) {
- if ($methodUS && $startDay != 30) {
- $endDay = 1;
- if ($endMonth == 12) {
- ++$endYear;
- $endMonth = 1;
- } else {
- ++$endMonth;
- }
- } else {
- $endDay = 30;
- }
- }
- return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
- } // function _dateDiff360()
- /**
- * _getDateValue
- *
- * @param string $dateValue
- * @return mixed Excel date/time serial value, or string if error
- */
- public static function _getDateValue($dateValue) {
- if (!is_numeric($dateValue)) {
- if ((is_string($dateValue)) && (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if ((is_object($dateValue)) && ($dateValue instanceof PHPExcel_Shared_Date::$dateTimeObjectType)) {
- $dateValue = PHPExcel_Shared_Date::PHPToExcel($dateValue);
- } else {
- $saveReturnDateType = PHPExcel_Calculation_Functions::getReturnDateType();
- PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
- $dateValue = self::DATEVALUE($dateValue);
- PHPExcel_Calculation_Functions::setReturnDateType($saveReturnDateType);
- }
- }
- return $dateValue;
- } // function _getDateValue()
- /**
- * _getTimeValue
- *
- * @param string $timeValue
- * @return mixed Excel date/time serial value, or string if error
- */
- private static function _getTimeValue($timeValue) {
- $saveReturnDateType = PHPExcel_Calculation_Functions::getReturnDateType();
- PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
- $timeValue = self::TIMEVALUE($timeValue);
- PHPExcel_Calculation_Functions::setReturnDateType($saveReturnDateType);
- return $timeValue;
- } // function _getTimeValue()
- private static function _adjustDateByMonths($dateValue = 0, $adjustmentMonths = 0) {
- // Execute function
- $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
- $oMonth = (int) $PHPDateObject->format('m');
- $oYear = (int) $PHPDateObject->format('Y');
- $adjustmentMonthsString = (string) $adjustmentMonths;
- if ($adjustmentMonths > 0) {
- $adjustmentMonthsString = '+'.$adjustmentMonths;
- }
- if ($adjustmentMonths != 0) {
- $PHPDateObject->modify($adjustmentMonthsString.' months');
- }
- $nMonth = (int) $PHPDateObject->format('m');
- $nYear = (int) $PHPDateObject->format('Y');
- $monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
- if ($monthDiff != $adjustmentMonths) {
- $adjustDays = (int) $PHPDateObject->format('d');
- $adjustDaysString = '-'.$adjustDays.' days';
- $PHPDateObject->modify($adjustDaysString);
- }
- return $PHPDateObject;
- } // function _adjustDateByMonths()
- /**
- * DATETIMENOW
- *
- * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
- * depending on the value of the ReturnDateType flag
- */
- public static function DATETIMENOW() {
- $saveTimeZone = date_default_timezone_get();
- date_default_timezone_set('UTC');
- $retValue = False;
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- $retValue = (float) PHPExcel_Shared_Date::PHPToExcel(time());
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- $retValue = (integer) time();
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- $retValue = new DateTime();
- break;
- }
- date_default_timezone_set($saveTimeZone);
- return $retValue;
- } // function DATETIMENOW()
- /**
- * DATENOW
- *
- * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
- * depending on the value of the ReturnDateType flag
- */
- public static function DATENOW() {
- $saveTimeZone = date_default_timezone_get();
- date_default_timezone_set('UTC');
- $retValue = False;
- $excelDateTime = floor(PHPExcel_Shared_Date::PHPToExcel(time()));
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- $retValue = (float) $excelDateTime;
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- $retValue = (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateTime) - 3600;
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- $retValue = PHPExcel_Shared_Date::ExcelToPHPObject($excelDateTime);
- break;
- }
- date_default_timezone_set($saveTimeZone);
- return $retValue;
- } // function DATENOW()
- /**
- * DATE
- *
- * @param long $year
- * @param long $month
- * @param long $day
- * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
- * depending on the value of the ReturnDateType flag
- */
- public static function DATE($year = 0, $month = 1, $day = 1) {
- $year = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($year);
- $month = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($month);
- $day = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($day);
- $baseYear = PHPExcel_Shared_Date::getExcelCalendar();
- // Validate parameters
- if ($year < ($baseYear-1900)) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- if ((($baseYear-1900) != 0) && ($year < $baseYear) && ($year >= 1900)) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- if (($year < $baseYear) && ($year >= ($baseYear-1900))) {
- $year += 1900;
- }
- if ($month < 1) {
- // Handle year/month adjustment if month < 1
- --$month;
- $year += ceil($month / 12) - 1;
- $month = 13 - abs($month % 12);
- } elseif ($month > 12) {
- // Handle year/month adjustment if month > 12
- $year += floor($month / 12);
- $month = ($month % 12);
- }
- // Re-validate the year parameter after adjustments
- if (($year < $baseYear) || ($year >= 10000)) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel($year, $month, $day);
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- return (float) $excelDateValue;
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- return (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateValue);
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- return PHPExcel_Shared_Date::ExcelToPHPObject($excelDateValue);
- break;
- }
- } // function DATE()
- /**
- * TIME
- *
- * @param long $hour
- * @param long $minute
- * @param long $second
- * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
- * depending on the value of the ReturnDateType flag
- */
- public static function TIME($hour = 0, $minute = 0, $second = 0) {
- $hour = PHPExcel_Calculation_Functions::flattenSingleValue($hour);
- $minute = PHPExcel_Calculation_Functions::flattenSingleValue($minute);
- $second = PHPExcel_Calculation_Functions::flattenSingleValue($second);
- if ($hour == '') { $hour = 0; }
- if ($minute == '') { $minute = 0; }
- if ($second == '') { $second = 0; }
- if ((!is_numeric($hour)) || (!is_numeric($minute)) || (!is_numeric($second))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- $hour = (integer) $hour;
- $minute = (integer) $minute;
- $second = (integer) $second;
- if ($second < 0) {
- $minute += floor($second / 60);
- $second = 60 - abs($second % 60);
- if ($second == 60) { $second = 0; }
- } elseif ($second >= 60) {
- $minute += floor($second / 60);
- $second = $second % 60;
- }
- if ($minute < 0) {
- $hour += floor($minute / 60);
- $minute = 60 - abs($minute % 60);
- if ($minute == 60) { $minute = 0; }
- } elseif ($minute >= 60) {
- $hour += floor($minute / 60);
- $minute = $minute % 60;
- }
- if ($hour > 23) {
- $hour = $hour % 24;
- } elseif ($hour < 0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- $date = 0;
- $calendar = PHPExcel_Shared_Date::getExcelCalendar();
- if ($calendar != PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900) {
- $date = 1;
- }
- return (float) PHPExcel_Shared_Date::FormattedPHPToExcel($calendar, 1, $date, $hour, $minute, $second);
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::FormattedPHPToExcel(1970, 1, 1, $hour-1, $minute, $second)); // -2147468400; // -2147472000 + 3600
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- $dayAdjust = 0;
- if ($hour < 0) {
- $dayAdjust = floor($hour / 24);
- $hour = 24 - abs($hour % 24);
- if ($hour == 24) { $hour = 0; }
- } elseif ($hour >= 24) {
- $dayAdjust = floor($hour / 24);
- $hour = $hour % 24;
- }
- $phpDateObject = new DateTime('1900-01-01 '.$hour.':'.$minute.':'.$second);
- if ($dayAdjust != 0) {
- $phpDateObject->modify($dayAdjust.' days');
- }
- return $phpDateObject;
- break;
- }
- } // function TIME()
- /**
- * DATEVALUE
- *
- * @param string $dateValue
- * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
- * depending on the value of the ReturnDateType flag
- */
- public static function DATEVALUE($dateValue = 1) {
- $dateValue = trim(PHPExcel_Calculation_Functions::flattenSingleValue($dateValue),'"');
- // Strip any ordinals because they're allowed in Excel (English only)
- $dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui','$1$3',$dateValue);
- // Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany)
- $dateValue = str_replace(array('/','.','-',' '),array(' ',' ',' ',' '),$dateValue);
- $yearFound = false;
- $t1 = explode(' ',$dateValue);
- foreach($t1 as &$t) {
- if ((is_numeric($t)) && ($t > 31)) {
- if ($yearFound) {
- return PHPExcel_Calculation_Functions::VALUE();
- } else {
- if ($t < 100) { $t += 1900; }
- $yearFound = true;
- }
- }
- }
- if ((count($t1) == 1) && (strpos($t,':') != false)) {
- // We've been fed a time value without any date
- return 0.0;
- } elseif (count($t1) == 2) {
- // We only have two parts of the date: either day/month or month/year
- if ($yearFound) {
- array_unshift($t1,1);
- } else {
- array_push($t1,date('Y'));
- }
- }
- unset($t);
- $dateValue = implode(' ',$t1);
- $PHPDateArray = date_parse($dateValue);
- if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
- $testVal1 = strtok($dateValue,'- ');
- if ($testVal1 !== False) {
- $testVal2 = strtok('- ');
- if ($testVal2 !== False) {
- $testVal3 = strtok('- ');
- if ($testVal3 === False) {
- $testVal3 = strftime('%Y');
- }
- } else {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- } else {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- $PHPDateArray = date_parse($testVal1.'-'.$testVal2.'-'.$testVal3);
- if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
- $PHPDateArray = date_parse($testVal2.'-'.$testVal1.'-'.$testVal3);
- if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- }
- }
- if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
- // Execute function
- if ($PHPDateArray['year'] == '') { $PHPDateArray['year'] = strftime('%Y'); }
- if ($PHPDateArray['month'] == '') { $PHPDateArray['month'] = strftime('%m'); }
- if ($PHPDateArray['day'] == '') { $PHPDateArray['day'] = strftime('%d'); }
- $excelDateValue = floor(PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray['year'],$PHPDateArray['month'],$PHPDateArray['day'],$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']));
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- return (float) $excelDateValue;
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- return (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateValue);
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- return new DateTime($PHPDateArray['year'].'-'.$PHPDateArray['month'].'-'.$PHPDateArray['day'].' 00:00:00');
- break;
- }
- }
- return PHPExcel_Calculation_Functions::VALUE();
- } // function DATEVALUE()
- /**
- * TIMEVALUE
- *
- * @param string $timeValue
- * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
- * depending on the value of the ReturnDateType flag
- */
- public static function TIMEVALUE($timeValue) {
- $timeValue = trim(PHPExcel_Calculation_Functions::flattenSingleValue($timeValue),'"');
- $timeValue = str_replace(array('/','.'),array('-','-'),$timeValue);
- $PHPDateArray = date_parse($timeValue);
- if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
- if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
- $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray['year'],$PHPDateArray['month'],$PHPDateArray['day'],$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']);
- } else {
- $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel(1900,1,1,$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']) - 1;
- }
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- return (float) $excelDateValue;
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- return (integer) $phpDateValue = PHPExcel_Shared_Date::ExcelToPHP($excelDateValue+25569) - 3600;;
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- return new DateTime('1900-01-01 '.$PHPDateArray['hour'].':'.$PHPDateArray['minute'].':'.$PHPDateArray['second']);
- break;
- }
- }
- return PHPExcel_Calculation_Functions::VALUE();
- } // function TIMEVALUE()
- /**
- * DATEDIF
- *
- * @param long $startDate Excel date serial value or a standard date string
- * @param long $endDate Excel date serial value or a standard date string
- * @param string $unit
- * @return long Interval between the dates
- */
- public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D') {
- $startDate = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
- $endDate = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
- $unit = strtoupper(PHPExcel_Calculation_Functions::flattenSingleValue($unit));
- if (is_string($startDate = self::_getDateValue($startDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (is_string($endDate = self::_getDateValue($endDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- // Validate parameters
- if ($startDate >= $endDate) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- $difference = $endDate - $startDate;
- $PHPStartDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($startDate);
- $startDays = $PHPStartDateObject->format('j');
- $startMonths = $PHPStartDateObject->format('n');
- $startYears = $PHPStartDateObject->format('Y');
- $PHPEndDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
- $endDays = $PHPEndDateObject->format('j');
- $endMonths = $PHPEndDateObject->format('n');
- $endYears = $PHPEndDateObject->format('Y');
- $retVal = PHPExcel_Calculation_Functions::NaN();
- switch ($unit) {
- case 'D':
- $retVal = intval($difference);
- break;
- case 'M':
- $retVal = intval($endMonths - $startMonths) + (intval($endYears - $startYears) * 12);
- // We're only interested in full months
- if ($endDays < $startDays) {
- --$retVal;
- }
- break;
- case 'Y':
- $retVal = intval($endYears - $startYears);
- // We're only interested in full months
- if ($endMonths < $startMonths) {
- --$retVal;
- } elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
- --$retVal;
- }
- break;
- case 'MD':
- if ($endDays < $startDays) {
- $retVal = $endDays;
- $PHPEndDateObject->modify('-'.$endDays.' days');
- $adjustDays = $PHPEndDateObject->format('j');
- if ($adjustDays > $startDays) {
- $retVal += ($adjustDays - $startDays);
- }
- } else {
- $retVal = $endDays - $startDays;
- }
- break;
- case 'YM':
- $retVal = intval($endMonths - $startMonths);
- if ($retVal < 0) $retVal = 12 + $retVal;
- // We're only interested in full months
- if ($endDays < $startDays) {
- --$retVal;
- }
- break;
- case 'YD':
- $retVal = intval($difference);
- if ($endYears > $startYears) {
- while ($endYears > $startYears) {
- $PHPEndDateObject->modify('-1 year');
- $endYears = $PHPEndDateObject->format('Y');
- }
- $retVal = $PHPEndDateObject->format('z') - $PHPStartDateObject->format('z');
- if ($retVal < 0) { $retVal += 365; }
- }
- break;
- }
- return $retVal;
- } // function DATEDIF()
- /**
- * DAYS360
- *
- * @param long $startDate Excel date serial value or a standard date string
- * @param long $endDate Excel date serial value or a standard date string
- * @param boolean $method US or European Method
- * @return long PHP date/time serial
- */
- public static function DAYS360($startDate = 0, $endDate = 0, $method = false) {
- $startDate = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
- $endDate = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
- if (is_string($startDate = self::_getDateValue($startDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (is_string($endDate = self::_getDateValue($endDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- // Execute function
- $PHPStartDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($startDate);
- $startDay = $PHPStartDateObject->format('j');
- $startMonth = $PHPStartDateObject->format('n');
- $startYear = $PHPStartDateObject->format('Y');
- $PHPEndDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
- $endDay = $PHPEndDateObject->format('j');
- $endMonth = $PHPEndDateObject->format('n');
- $endYear = $PHPEndDateObject->format('Y');
- return self::_dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, !$method);
- } // function DAYS360()
- /**
- * YEARFRAC
- *
- * Calculates the fraction of the year represented by the number of whole days between two dates (the start_date and the
- * end_date). Use the YEARFRAC worksheet function to identify the proportion of a whole year's benefits or obligations
- * to assign to a specific term.
- *
- * @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer) or date object, or a standard date string
- * @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer) or date object, or a standard date string
- * @param integer $method Method used for the calculation
- * 0 or omitted US (NASD) 30/360
- * 1 Actual/actual
- * 2 Actual/360
- * 3 Actual/365
- * 4 European 30/360
- * @return float fraction of the year
- */
- public static function YEARFRAC($startDate = 0, $endDate = 0, $method = 0) {
- $startDate = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
- $endDate = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
- $method = PHPExcel_Calculation_Functions::flattenSingleValue($method);
- if (is_string($startDate = self::_getDateValue($startDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (is_string($endDate = self::_getDateValue($endDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (((is_numeric($method)) && (!is_string($method))) || ($method == '')) {
- switch($method) {
- case 0 :
- return self::DAYS360($startDate,$endDate) / 360;
- break;
- case 1 :
- $days = self::DATEDIF($startDate,$endDate);
- $startYear = self::YEAR($startDate);
- $endYear = self::YEAR($endDate);
- $years = $endYear - $startYear + 1;
- $leapDays = 0;
- if ($years == 1) {
- if (self::_isLeapYear($endYear)) {
- $startMonth = self::MONTHOFYEAR($startDate);
- $endMonth = self::MONTHOFYEAR($endDate);
- $endDay = self::DAYOFMONTH($endDate);
- if (($startMonth < 3) ||
- (($endMonth * 100 + $endDay) >= (2 * 100 + 29))) {
- $leapDays += 1;
- }
- }
- } else {
- for($year = $startYear; $year <= $endYear; ++$year) {
- if ($year == $startYear) {
- $startMonth = self::MONTHOFYEAR($startDate);
- $startDay = self::DAYOFMONTH($startDate);
- if ($startMonth < 3) {
- $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
- }
- } elseif($year == $endYear) {
- $endMonth = self::MONTHOFYEAR($endDate);
- $endDay = self::DAYOFMONTH($endDate);
- if (($endMonth * 100 + $endDay) >= (2 * 100 + 29)) {
- $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
- }
- } else {
- $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
- }
- }
- if ($years == 2) {
- if (($leapDays == 0) && (self::_isLeapYear($startYear)) && ($days > 365)) {
- $leapDays = 1;
- } elseif ($days < 366) {
- $years = 1;
- }
- }
- $leapDays /= $years;
- }
- return $days / (365 + $leapDays);
- break;
- case 2 :
- return self::DATEDIF($startDate,$endDate) / 360;
- break;
- case 3 :
- return self::DATEDIF($startDate,$endDate) / 365;
- break;
- case 4 :
- return self::DAYS360($startDate,$endDate,True) / 360;
- break;
- }
- }
- return PHPExcel_Calculation_Functions::VALUE();
- } // function YEARFRAC()
- /**
- * NETWORKDAYS
- *
- * @param mixed Start date
- * @param mixed End date
- * @param array of mixed Optional Date Series
- * @return long Interval between the dates
- */
- public static function NETWORKDAYS($startDate,$endDate) {
- // Retrieve the mandatory start and end date that are referenced in the function definition
- $startDate = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
- $endDate = PHPExcel_Calculation_Functions::flattenSingleValue($endDate);
- // Flush the mandatory start and end date that are referenced in the function definition, and get the optional days
- $dateArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- array_shift($dateArgs);
- array_shift($dateArgs);
- // Validate the start and end dates
- if (is_string($startDate = $sDate = self::_getDateValue($startDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- $startDate = (float) floor($startDate);
- if (is_string($endDate = $eDate = self::_getDateValue($endDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- $endDate = (float) floor($endDate);
- if ($sDate > $eDate) {
- $startDate = $eDate;
- $endDate = $sDate;
- }
- // Execute function
- $startDoW = 6 - self::DAYOFWEEK($startDate,2);
- if ($startDoW < 0) { $startDoW = 0; }
- $endDoW = self::DAYOFWEEK($endDate,2);
- if ($endDoW >= 6) { $endDoW = 0; }
- $wholeWeekDays = floor(($endDate - $startDate) / 7) * 5;
- $partWeekDays = $endDoW + $startDoW;
- if ($partWeekDays > 5) {
- $partWeekDays -= 5;
- }
- // Test any extra holiday parameters
- $holidayCountedArray = array();
- foreach ($dateArgs as $holidayDate) {
- if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
- if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
- --$partWeekDays;
- $holidayCountedArray[] = $holidayDate;
- }
- }
- }
- if ($sDate > $eDate) {
- return 0 - ($wholeWeekDays + $partWeekDays);
- }
- return $wholeWeekDays + $partWeekDays;
- } // function NETWORKDAYS()
- /**
- * WORKDAY
- *
- * @param mixed Start date
- * @param mixed number of days for adjustment
- * @param array of mixed Optional Date Series
- * @return long Interval between the dates
- */
- public static function WORKDAY($startDate,$endDays) {
- // Retrieve the mandatory start date and days that are referenced in the function definition
- $startDate = PHPExcel_Calculation_Functions::flattenSingleValue($startDate);
- $endDays = (int) PHPExcel_Calculation_Functions::flattenSingleValue($endDays);
- // Flush the mandatory start date and days that are referenced in the function definition, and get the optional days
- $dateArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
- array_shift($dateArgs);
- array_shift($dateArgs);
- if ((is_string($startDate = self::_getDateValue($startDate))) || (!is_numeric($endDays))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- $startDate = (float) floor($startDate);
- // If endDays is 0, we always return startDate
- if ($endDays == 0) { return $startDate; }
- $decrementing = ($endDays < 0) ? True : False;
- // Adjust the start date if it falls over a weekend
- $startDoW = self::DAYOFWEEK($startDate,3);
- if (self::DAYOFWEEK($startDate,3) >= 5) {
- $startDate += ($decrementing) ? -$startDoW + 4: 7 - $startDoW;
- ($decrementing) ? $endDays++ : $endDays--;
- }
- // Add endDays
- $endDate = (float) $startDate + (intval($endDays / 5) * 7) + ($endDays % 5);
- // Adjust the calculated end date if it falls over a weekend
- $endDoW = self::DAYOFWEEK($endDate,3);
- if ($endDoW >= 5) {
- $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
- }
- // Test any extra holiday parameters
- if (count($dateArgs) > 0) {
- $holidayCountedArray = $holidayDates = array();
- foreach ($dateArgs as $holidayDate) {
- if ((!is_null($holidayDate)) && (trim($holidayDate) > '')) {
- if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (self::DAYOFWEEK($holidayDate,3) < 5) {
- $holidayDates[] = $holidayDate;
- }
- }
- }
- if ($decrementing) {
- rsort($holidayDates, SORT_NUMERIC);
- } else {
- sort($holidayDates, SORT_NUMERIC);
- }
- foreach ($holidayDates as $holidayDate) {
- if ($decrementing) {
- if (($holidayDate <= $startDate) && ($holidayDate >= $endDate)) {
- if (!in_array($holidayDate,$holidayCountedArray)) {
- --$endDate;
- $holidayCountedArray[] = $holidayDate;
- }
- }
- } else {
- if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
- if (!in_array($holidayDate,$holidayCountedArray)) {
- ++$endDate;
- $holidayCountedArray[] = $holidayDate;
- }
- }
- }
- // Adjust the calculated end date if it falls over a weekend
- $endDoW = self::DAYOFWEEK($endDate,3);
- if ($endDoW >= 5) {
- $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
- }
- }
- }
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- return (float) $endDate;
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- return (integer) PHPExcel_Shared_Date::ExcelToPHP($endDate);
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- return PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
- break;
- }
- } // function WORKDAY()
- /**
- * DAYOFMONTH
- *
- * @param long $dateValue Excel date serial value or a standard date string
- * @return int Day
- */
- public static function DAYOFMONTH($dateValue = 1) {
- $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
- if (is_string($dateValue = self::_getDateValue($dateValue))) {
- return PHPExcel_Calculation_Functions::VALUE();
- } elseif ($dateValue == 0.0) {
- return 0;
- } elseif ($dateValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
- return (int) $PHPDateObject->format('j');
- } // function DAYOFMONTH()
- /**
- * DAYOFWEEK
- *
- * @param long $dateValue Excel date serial value or a standard date string
- * @return int Day
- */
- public static function DAYOFWEEK($dateValue = 1, $style = 1) {
- $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
- $style = floor(PHPExcel_Calculation_Functions::flattenSingleValue($style));
- if (is_string($dateValue = self::_getDateValue($dateValue))) {
- return PHPExcel_Calculation_Functions::VALUE();
- } elseif ($dateValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
- $DoW = $PHPDateObject->format('w');
- $firstDay = 1;
- switch ($style) {
- case 1: ++$DoW;
- break;
- case 2: if ($DoW == 0) { $DoW = 7; }
- break;
- case 3: if ($DoW == 0) { $DoW = 7; }
- $firstDay = 0;
- --$DoW;
- break;
- default:
- }
- if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL) {
- // Test for Excel's 1900 leap year, and introduce the error as required
- if (($PHPDateObject->format('Y') == 1900) && ($PHPDateObject->format('n') <= 2)) {
- --$DoW;
- if ($DoW < $firstDay) {
- $DoW += 7;
- }
- }
- }
- return (int) $DoW;
- } // function DAYOFWEEK()
- /**
- * WEEKOFYEAR
- *
- * @param long $dateValue Excel date serial value or a standard date string
- * @param boolean $method Week begins on Sunday or Monday
- * @return int Week Number
- */
- public static function WEEKOFYEAR($dateValue = 1, $method = 1) {
- $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
- $method = floor(PHPExcel_Calculation_Functions::flattenSingleValue($method));
- if (!is_numeric($method)) {
- return PHPExcel_Calculation_Functions::VALUE();
- } elseif (($method < 1) || ($method > 2)) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- if (is_string($dateValue = self::_getDateValue($dateValue))) {
- return PHPExcel_Calculation_Functions::VALUE();
- } elseif ($dateValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
- $dayOfYear = $PHPDateObject->format('z');
- $dow = $PHPDateObject->format('w');
- $PHPDateObject->modify('-'.$dayOfYear.' days');
- $dow = $PHPDateObject->format('w');
- $daysInFirstWeek = 7 - (($dow + (2 - $method)) % 7);
- $dayOfYear -= $daysInFirstWeek;
- $weekOfYear = ceil($dayOfYear / 7) + 1;
- return (int) $weekOfYear;
- } // function WEEKOFYEAR()
- /**
- * MONTHOFYEAR
- *
- * @param long $dateValue Excel date serial value or a standard date string
- * @return int Month
- */
- public static function MONTHOFYEAR($dateValue = 1) {
- $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
- if (is_string($dateValue = self::_getDateValue($dateValue))) {
- return PHPExcel_Calculation_Functions::VALUE();
- } elseif ($dateValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
- return (int) $PHPDateObject->format('n');
- } // function MONTHOFYEAR()
- /**
- * YEAR
- *
- * @param long $dateValue Excel date serial value or a standard date string
- * @return int Year
- */
- public static function YEAR($dateValue = 1) {
- $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
- if (is_string($dateValue = self::_getDateValue($dateValue))) {
- return PHPExcel_Calculation_Functions::VALUE();
- } elseif ($dateValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- // Execute function
- $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
- return (int) $PHPDateObject->format('Y');
- } // function YEAR()
- /**
- * HOUROFDAY
- *
- * @param mixed $timeValue Excel time serial value or a standard time string
- * @return int Hour
- */
- public static function HOUROFDAY($timeValue = 0) {
- $timeValue = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
- if (!is_numeric($timeValue)) {
- if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
- $testVal = strtok($timeValue,'/-: ');
- if (strlen($testVal) < strlen($timeValue)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- }
- $timeValue = self::_getTimeValue($timeValue);
- if (is_string($timeValue)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- }
- // Execute function
- if ($timeValue >= 1) {
- $timeValue = fmod($timeValue,1);
- } elseif ($timeValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
- return (int) gmdate('G',$timeValue);
- } // function HOUROFDAY()
- /**
- * MINUTEOFHOUR
- *
- * @param long $timeValue Excel time serial value or a standard time string
- * @return int Minute
- */
- public static function MINUTEOFHOUR($timeValue = 0) {
- $timeValue = $timeTester = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
- if (!is_numeric($timeValue)) {
- if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
- $testVal = strtok($timeValue,'/-: ');
- if (strlen($testVal) < strlen($timeValue)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- }
- $timeValue = self::_getTimeValue($timeValue);
- if (is_string($timeValue)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- }
- // Execute function
- if ($timeValue >= 1) {
- $timeValue = fmod($timeValue,1);
- } elseif ($timeValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
- return (int) gmdate('i',$timeValue);
- } // function MINUTEOFHOUR()
- /**
- * SECONDOFMINUTE
- *
- * @param long $timeValue Excel time serial value or a standard time string
- * @return int Second
- */
- public static function SECONDOFMINUTE($timeValue = 0) {
- $timeValue = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
- if (!is_numeric($timeValue)) {
- if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
- $testVal = strtok($timeValue,'/-: ');
- if (strlen($testVal) < strlen($timeValue)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- }
- $timeValue = self::_getTimeValue($timeValue);
- if (is_string($timeValue)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- }
- // Execute function
- if ($timeValue >= 1) {
- $timeValue = fmod($timeValue,1);
- } elseif ($timeValue < 0.0) {
- return PHPExcel_Calculation_Functions::NaN();
- }
- $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
- return (int) gmdate('s',$timeValue);
- } // function SECONDOFMINUTE()
- /**
- * EDATE
- *
- * Returns the serial number that represents the date that is the indicated number of months before or after a specified date
- * (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.
- *
- * @param long $dateValue Excel date serial value or a standard date string
- * @param int $adjustmentMonths Number of months to adjust by
- * @return long Excel date serial value
- */
- public static function EDATE($dateValue = 1, $adjustmentMonths = 0) {
- $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
- $adjustmentMonths = floor(PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths));
- if (!is_numeric($adjustmentMonths)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (is_string($dateValue = self::_getDateValue($dateValue))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- // Execute function
- $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths);
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- return $PHPDateObject;
- break;
- }
- } // function EDATE()
- /**
- * EOMONTH
- *
- * Returns the serial number for the last day of the month that is the indicated number of months before or after start_date.
- * Use EOMONTH to calculate maturity dates or due dates that fall on the last day of the month.
- *
- * @param long $dateValue Excel date serial value or a standard date string
- * @param int $adjustmentMonths Number of months to adjust by
- * @return long Excel date serial value
- */
- public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) {
- $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
- $adjustmentMonths = floor(PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths));
- if (!is_numeric($adjustmentMonths)) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- if (is_string($dateValue = self::_getDateValue($dateValue))) {
- return PHPExcel_Calculation_Functions::VALUE();
- }
- // Execute function
- $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths+1);
- $adjustDays = (int) $PHPDateObject->format('d');
- $adjustDaysString = '-'.$adjustDays.' days';
- $PHPDateObject->modify($adjustDaysString);
- switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
- case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
- return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
- return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
- break;
- case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
- return $PHPDateObject;
- break;
- }
- } // function EOMONTH()
- } // class PHPExcel_Calculation_DateTime
|