DateTime.php 39 KB

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