PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/extensions/PHPExcel/vendor/PHPExcel/Calculation/DateTime.php

https://bitbucket.org/y_widyatama/ijepa2
PHP | 1475 lines | 799 code | 141 blank | 535 comment | 241 complexity | b1c9f1c0f0cbc353414dac5cdea36a0a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause

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

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