2f27774244379d384e9ac9bc3835cd1d58135ead.svn-base 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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 ($dateValue === null) {
  985. $dateValue = 1;
  986. } elseif (is_string($dateValue = self::_getDateValue($dateValue))) {
  987. return PHPExcel_Calculation_Functions::VALUE();
  988. } elseif ($dateValue == 0.0) {
  989. return 0;
  990. } elseif ($dateValue < 0.0) {
  991. return PHPExcel_Calculation_Functions::NaN();
  992. }
  993. // Execute function
  994. $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  995. return (int) $PHPDateObject->format('j');
  996. } // function DAYOFMONTH()
  997. /**
  998. * DAYOFWEEK
  999. *
  1000. * Returns the day of the week for a specified date. The day is given as an integer
  1001. * ranging from 0 to 7 (dependent on the requested style).
  1002. *
  1003. * Excel Function:
  1004. * WEEKDAY(dateValue[,style])
  1005. *
  1006. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  1007. * PHP DateTime object, or a standard date string
  1008. * @param int $style A number that determines the type of return value
  1009. * 1 or omitted Numbers 1 (Sunday) through 7 (Saturday).
  1010. * 2 Numbers 1 (Monday) through 7 (Sunday).
  1011. * 3 Numbers 0 (Monday) through 6 (Sunday).
  1012. * @return int Day of the week value
  1013. */
  1014. public static function DAYOFWEEK($dateValue = 1, $style = 1) {
  1015. $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1016. $style = PHPExcel_Calculation_Functions::flattenSingleValue($style);
  1017. if (!is_numeric($style)) {
  1018. return PHPExcel_Calculation_Functions::VALUE();
  1019. } elseif (($style < 1) || ($style > 3)) {
  1020. return PHPExcel_Calculation_Functions::NaN();
  1021. }
  1022. $style = floor($style);
  1023. if ($dateValue === null) {
  1024. $dateValue = 1;
  1025. } elseif (is_string($dateValue = self::_getDateValue($dateValue))) {
  1026. return PHPExcel_Calculation_Functions::VALUE();
  1027. } elseif ($dateValue < 0.0) {
  1028. return PHPExcel_Calculation_Functions::NaN();
  1029. }
  1030. // Execute function
  1031. $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  1032. $DoW = $PHPDateObject->format('w');
  1033. $firstDay = 1;
  1034. switch ($style) {
  1035. case 1: ++$DoW;
  1036. break;
  1037. case 2: if ($DoW == 0) { $DoW = 7; }
  1038. break;
  1039. case 3: if ($DoW == 0) { $DoW = 7; }
  1040. $firstDay = 0;
  1041. --$DoW;
  1042. break;
  1043. }
  1044. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL) {
  1045. // Test for Excel's 1900 leap year, and introduce the error as required
  1046. if (($PHPDateObject->format('Y') == 1900) && ($PHPDateObject->format('n') <= 2)) {
  1047. --$DoW;
  1048. if ($DoW < $firstDay) {
  1049. $DoW += 7;
  1050. }
  1051. }
  1052. }
  1053. return (int) $DoW;
  1054. } // function DAYOFWEEK()
  1055. /**
  1056. * WEEKOFYEAR
  1057. *
  1058. * Returns the week of the year for a specified date.
  1059. * The WEEKNUM function considers the week containing January 1 to be the first week of the year.
  1060. * However, there is a European standard that defines the first week as the one with the majority
  1061. * of days (four or more) falling in the new year. This means that for years in which there are
  1062. * three days or less in the first week of January, the WEEKNUM function returns week numbers
  1063. * that are incorrect according to the European standard.
  1064. *
  1065. * Excel Function:
  1066. * WEEKNUM(dateValue[,style])
  1067. *
  1068. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  1069. * PHP DateTime object, or a standard date string
  1070. * @param boolean $method Week begins on Sunday or Monday
  1071. * 1 or omitted Week begins on Sunday.
  1072. * 2 Week begins on Monday.
  1073. * @return int Week Number
  1074. */
  1075. public static function WEEKOFYEAR($dateValue = 1, $method = 1) {
  1076. $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1077. $method = PHPExcel_Calculation_Functions::flattenSingleValue($method);
  1078. if (!is_numeric($method)) {
  1079. return PHPExcel_Calculation_Functions::VALUE();
  1080. } elseif (($method < 1) || ($method > 2)) {
  1081. return PHPExcel_Calculation_Functions::NaN();
  1082. }
  1083. $method = floor($method);
  1084. if ($dateValue === null) {
  1085. $dateValue = 1;
  1086. } elseif (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. $dayOfYear = $PHPDateObject->format('z');
  1094. $dow = $PHPDateObject->format('w');
  1095. $PHPDateObject->modify('-'.$dayOfYear.' days');
  1096. $dow = $PHPDateObject->format('w');
  1097. $daysInFirstWeek = 7 - (($dow + (2 - $method)) % 7);
  1098. $dayOfYear -= $daysInFirstWeek;
  1099. $weekOfYear = ceil($dayOfYear / 7) + 1;
  1100. return (int) $weekOfYear;
  1101. } // function WEEKOFYEAR()
  1102. /**
  1103. * MONTHOFYEAR
  1104. *
  1105. * Returns the month of a date represented by a serial number.
  1106. * The month is given as an integer, ranging from 1 (January) to 12 (December).
  1107. *
  1108. * Excel Function:
  1109. * MONTH(dateValue)
  1110. *
  1111. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  1112. * PHP DateTime object, or a standard date string
  1113. * @return int Month of the year
  1114. */
  1115. public static function MONTHOFYEAR($dateValue = 1) {
  1116. $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1117. if ($dateValue === null) {
  1118. $dateValue = 1;
  1119. } elseif (is_string($dateValue = self::_getDateValue($dateValue))) {
  1120. return PHPExcel_Calculation_Functions::VALUE();
  1121. } elseif ($dateValue < 0.0) {
  1122. return PHPExcel_Calculation_Functions::NaN();
  1123. }
  1124. // Execute function
  1125. $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  1126. return (int) $PHPDateObject->format('n');
  1127. } // function MONTHOFYEAR()
  1128. /**
  1129. * YEAR
  1130. *
  1131. * Returns the year corresponding to a date.
  1132. * The year is returned as an integer in the range 1900-9999.
  1133. *
  1134. * Excel Function:
  1135. * YEAR(dateValue)
  1136. *
  1137. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  1138. * PHP DateTime object, or a standard date string
  1139. * @return int Year
  1140. */
  1141. public static function YEAR($dateValue = 1) {
  1142. $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1143. if ($dateValue === null) {
  1144. $dateValue = 1;
  1145. } elseif (is_string($dateValue = self::_getDateValue($dateValue))) {
  1146. return PHPExcel_Calculation_Functions::VALUE();
  1147. } elseif ($dateValue < 0.0) {
  1148. return PHPExcel_Calculation_Functions::NaN();
  1149. }
  1150. // Execute function
  1151. $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
  1152. return (int) $PHPDateObject->format('Y');
  1153. } // function YEAR()
  1154. /**
  1155. * HOUROFDAY
  1156. *
  1157. * Returns the hour of a time value.
  1158. * The hour is given as an integer, ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.).
  1159. *
  1160. * Excel Function:
  1161. * HOUR(timeValue)
  1162. *
  1163. * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
  1164. * PHP DateTime object, or a standard time string
  1165. * @return int Hour
  1166. */
  1167. public static function HOUROFDAY($timeValue = 0) {
  1168. $timeValue = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
  1169. if (!is_numeric($timeValue)) {
  1170. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1171. $testVal = strtok($timeValue,'/-: ');
  1172. if (strlen($testVal) < strlen($timeValue)) {
  1173. return PHPExcel_Calculation_Functions::VALUE();
  1174. }
  1175. }
  1176. $timeValue = self::_getTimeValue($timeValue);
  1177. if (is_string($timeValue)) {
  1178. return PHPExcel_Calculation_Functions::VALUE();
  1179. }
  1180. }
  1181. // Execute function
  1182. if ($timeValue >= 1) {
  1183. $timeValue = fmod($timeValue,1);
  1184. } elseif ($timeValue < 0.0) {
  1185. return PHPExcel_Calculation_Functions::NaN();
  1186. }
  1187. $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
  1188. return (int) gmdate('G',$timeValue);
  1189. } // function HOUROFDAY()
  1190. /**
  1191. * MINUTEOFHOUR
  1192. *
  1193. * Returns the minutes of a time value.
  1194. * The minute is given as an integer, ranging from 0 to 59.
  1195. *
  1196. * Excel Function:
  1197. * MINUTE(timeValue)
  1198. *
  1199. * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
  1200. * PHP DateTime object, or a standard time string
  1201. * @return int Minute
  1202. */
  1203. public static function MINUTEOFHOUR($timeValue = 0) {
  1204. $timeValue = $timeTester = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
  1205. if (!is_numeric($timeValue)) {
  1206. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1207. $testVal = strtok($timeValue,'/-: ');
  1208. if (strlen($testVal) < strlen($timeValue)) {
  1209. return PHPExcel_Calculation_Functions::VALUE();
  1210. }
  1211. }
  1212. $timeValue = self::_getTimeValue($timeValue);
  1213. if (is_string($timeValue)) {
  1214. return PHPExcel_Calculation_Functions::VALUE();
  1215. }
  1216. }
  1217. // Execute function
  1218. if ($timeValue >= 1) {
  1219. $timeValue = fmod($timeValue,1);
  1220. } elseif ($timeValue < 0.0) {
  1221. return PHPExcel_Calculation_Functions::NaN();
  1222. }
  1223. $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
  1224. return (int) gmdate('i',$timeValue);
  1225. } // function MINUTEOFHOUR()
  1226. /**
  1227. * SECONDOFMINUTE
  1228. *
  1229. * Returns the seconds of a time value.
  1230. * The second is given as an integer in the range 0 (zero) to 59.
  1231. *
  1232. * Excel Function:
  1233. * SECOND(timeValue)
  1234. *
  1235. * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
  1236. * PHP DateTime object, or a standard time string
  1237. * @return int Second
  1238. */
  1239. public static function SECONDOFMINUTE($timeValue = 0) {
  1240. $timeValue = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
  1241. if (!is_numeric($timeValue)) {
  1242. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1243. $testVal = strtok($timeValue,'/-: ');
  1244. if (strlen($testVal) < strlen($timeValue)) {
  1245. return PHPExcel_Calculation_Functions::VALUE();
  1246. }
  1247. }
  1248. $timeValue = self::_getTimeValue($timeValue);
  1249. if (is_string($timeValue)) {
  1250. return PHPExcel_Calculation_Functions::VALUE();
  1251. }
  1252. }
  1253. // Execute function
  1254. if ($timeValue >= 1) {
  1255. $timeValue = fmod($timeValue,1);
  1256. } elseif ($timeValue < 0.0) {
  1257. return PHPExcel_Calculation_Functions::NaN();
  1258. }
  1259. $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
  1260. return (int) gmdate('s',$timeValue);
  1261. } // function SECONDOFMINUTE()
  1262. /**
  1263. * EDATE
  1264. *
  1265. * Returns the serial number that represents the date that is the indicated number of months
  1266. * before or after a specified date (the start_date).
  1267. * Use EDATE to calculate maturity dates or due dates that fall on the same day of the month
  1268. * as the date of issue.
  1269. *
  1270. * Excel Function:
  1271. * EDATE(dateValue,adjustmentMonths)
  1272. *
  1273. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  1274. * PHP DateTime object, or a standard date string
  1275. * @param int $adjustmentMonths The number of months before or after start_date.
  1276. * A positive value for months yields a future date;
  1277. * a negative value yields a past date.
  1278. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  1279. * depending on the value of the ReturnDateType flag
  1280. */
  1281. public static function EDATE($dateValue = 1, $adjustmentMonths = 0) {
  1282. $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1283. $adjustmentMonths = PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths);
  1284. if (!is_numeric($adjustmentMonths)) {
  1285. return PHPExcel_Calculation_Functions::VALUE();
  1286. }
  1287. $adjustmentMonths = floor($adjustmentMonths);
  1288. if (is_string($dateValue = self::_getDateValue($dateValue))) {
  1289. return PHPExcel_Calculation_Functions::VALUE();
  1290. }
  1291. // Execute function
  1292. $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths);
  1293. switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  1294. case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  1295. return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
  1296. case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  1297. return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
  1298. case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  1299. return $PHPDateObject;
  1300. }
  1301. } // function EDATE()
  1302. /**
  1303. * EOMONTH
  1304. *
  1305. * Returns the date value for the last day of the month that is the indicated number of months
  1306. * before or after start_date.
  1307. * Use EOMONTH to calculate maturity dates or due dates that fall on the last day of the month.
  1308. *
  1309. * Excel Function:
  1310. * EOMONTH(dateValue,adjustmentMonths)
  1311. *
  1312. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  1313. * PHP DateTime object, or a standard date string
  1314. * @param int $adjustmentMonths The number of months before or after start_date.
  1315. * A positive value for months yields a future date;
  1316. * a negative value yields a past date.
  1317. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  1318. * depending on the value of the ReturnDateType flag
  1319. */
  1320. public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) {
  1321. $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
  1322. $adjustmentMonths = PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths);
  1323. if (!is_numeric($adjustmentMonths)) {
  1324. return PHPExcel_Calculation_Functions::VALUE();
  1325. }
  1326. $adjustmentMonths = floor($adjustmentMonths);
  1327. if (is_string($dateValue = self::_getDateValue($dateValue))) {
  1328. return PHPExcel_Calculation_Functions::VALUE();
  1329. }
  1330. // Execute function
  1331. $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths+1);
  1332. $adjustDays = (int) $PHPDateObject->format('d');
  1333. $adjustDaysString = '-'.$adjustDays.' days';
  1334. $PHPDateObject->modify($adjustDaysString);
  1335. switch (PHPExcel_Calculation_Functions::getReturnDateType()) {
  1336. case PHPExcel_Calculation_Functions::RETURNDATE_EXCEL :
  1337. return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
  1338. case PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC :
  1339. return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
  1340. case PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT :
  1341. return $PHPDateObject;
  1342. }
  1343. } // function EOMONTH()
  1344. } // class PHPExcel_Calculation_DateTime