PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Classes/PHPExcel/Calculation/DateTime.php

https://github.com/iGroup/PHPExcel
PHP | 1447 lines | 790 code | 138 blank | 519 comment | 236 complexity | 89f0e8337da92bb9cd737de0cd7cacc4 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file