DateTime.php 69 KB

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