You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

calculation-engine.md 61 KiB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. # Calculation Engine
  2. ## Using the PhpSpreadsheet calculation engine
  3. ### Performing formula calculations
  4. As PhpSpreadsheet represents an in-memory spreadsheet, it also offers
  5. formula calculation capabilities. A cell can be of a value type
  6. (containing a number or text), or a formula type (containing a formula
  7. which can be evaluated). For example, the formula `=SUM(A1:A10)`
  8. evaluates to the sum of values in A1, A2, ..., A10.
  9. To calculate a formula, you can call the cell containing the formula’s
  10. method `getCalculatedValue()`, for example:
  11. ``` php
  12. $spreadsheet->getActiveSheet()->getCell('E11')->getCalculatedValue();
  13. ```
  14. If you write the following line of code in the invoice demo included
  15. with PhpSpreadsheet, it evaluates to the value "64":
  16. ![09-command-line-calculation.png](./images/09-command-line-calculation.png)
  17. Another nice feature of PhpSpreadsheet's formula parser, is that it can
  18. automatically adjust a formula when inserting/removing rows/columns.
  19. Here's an example:
  20. ![09-formula-in-cell-1.png](./images/09-formula-in-cell-1.png)
  21. You see that the formula contained in cell E11 is "SUM(E4:E9)". Now,
  22. when I write the following line of code, two new product lines are
  23. added:
  24. ``` php
  25. $spreadsheet->getActiveSheet()->insertNewRowBefore(7, 2);
  26. ```
  27. ![09-formula-in-cell-2.png](./images/09-formula-in-cell-2.png)
  28. Did you notice? The formula in the former cell E11 (now E13, as I
  29. inserted 2 new rows), changed to "SUM(E4:E11)". Also, the inserted cells
  30. duplicate style information of the previous cell, just like Excel's
  31. behaviour. Note that you can both insert rows and columns.
  32. ## Calculation Cache
  33. Once the Calculation engine has evaluated the formula in a cell, the result
  34. will be cached, so if you call `getCalculatedValue()` a second time for the
  35. same cell, the result will be returned from the cache rather than evaluating
  36. the formula a second time. This helps boost performance, because evaluating
  37. a formula is an expensive operation in terms of performance and speed.
  38. However, there may be times when you don't want this, perhaps you've changed
  39. the underlying data and need to re-evaluate the same formula with that new
  40. data.
  41. ```
  42. Calculation::getInstance($spreadsheet)->disableCalculationCache();
  43. ```
  44. Will disable calculation caching, and flush the current calculation cache.
  45. If you want only to flush the cache, then you can call
  46. ```
  47. Calculation::getInstance($spreadsheet)->clearCalculationCache();
  48. ```
  49. ## Known limitations
  50. There are some known limitations to the PhpSpreadsheet calculation
  51. engine. Most of them are due to the fact that an Excel formula is
  52. converted into PHP code before being executed. This means that Excel
  53. formula calculation is subject to PHP's language characteristics.
  54. ### Function that are not Supported in Xls
  55. Not all functions are supported, for a comprehensive list, read the
  56. [function list by name](../references/function-list-by-name.md).
  57. #### Operator precedence
  58. In Excel `+` wins over `&`, just like `*` wins over `+` in ordinary
  59. algebra. The former rule is not what one finds using the calculation
  60. engine shipped with PhpSpreadsheet.
  61. - [Reference for Excel](https://support.office.com/en-us/article/Calculation-operators-and-precedence-in-Excel-48be406d-4975-4d31-b2b8-7af9e0e2878a)
  62. - [Reference for PHP](https://php.net/manual/en/language.operators.php)
  63. #### Formulas involving numbers and text
  64. Formulas involving numbers and text may produce unexpected results or
  65. even unreadable file contents. For example, the formula `=3+"Hello "` is
  66. expected to produce an error in Excel (\#VALUE!). Due to the fact that
  67. PHP converts `"Hello "` to a numeric value (zero), the result of this
  68. formula is evaluated as 3 instead of evaluating as an error. This also
  69. causes the Excel document being generated as containing unreadable
  70. content.
  71. - [Reference for this behaviour in PHP](https://php.net/manual/en/language.types.string.php#language.types.string.conversion)
  72. #### Formulas don’t seem to be calculated in Excel2003 using compatibility pack?
  73. This is normal behaviour of the compatibility pack, Xlsx displays this
  74. correctly. Use `\PhpOffice\PhpSpreadsheet\Writer\Xls` if you really need
  75. calculated values, or force recalculation in Excel2003.
  76. ## Handling Date and Time Values
  77. ### Excel functions that return a Date and Time value
  78. Any of the Date and Time functions that return a date value in Excel can
  79. return either an Excel timestamp or a PHP timestamp or `DateTime` object.
  80. It is possible for scripts to change the data type used for returning
  81. date values by calling the
  82. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType()`
  83. method:
  84. ``` php
  85. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($returnDateType);
  86. ```
  87. where the following constants can be used for `$returnDateType`:
  88. - `\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC`
  89. - `\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_OBJECT`
  90. - `\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL`
  91. The method will return a Boolean True on success, False on failure (e.g.
  92. if an invalid value is passed in for the return date type).
  93. The `\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`
  94. method can be used to determine the current value of this setting:
  95. ``` php
  96. $returnDateType = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType();
  97. ```
  98. The default is `RETURNDATE_PHP_NUMERIC`.
  99. #### PHP Timestamps
  100. If `RETURNDATE_PHP_NUMERIC` is set for the Return Date Type, then any
  101. date value returned to the calling script by any access to the Date and
  102. Time functions in Excel will be an integer value that represents the
  103. number of seconds from the PHP/Unix base date. The PHP/Unix base date
  104. (0) is 00:00 UST on 1st January 1970. This value can be positive or
  105. negative: so a value of -3600 would be 23:00 hrs on 31st December 1969;
  106. while a value of +3600 would be 01:00 hrs on 1st January 1970. This
  107. gives PHP a date range of between 14th December 1901 and 19th January
  108. 2038.
  109. #### PHP `DateTime` Objects
  110. If the Return Date Type is set for `RETURNDATE_PHP_OBJECT`, then any
  111. date value returned to the calling script by any access to the Date and
  112. Time functions in Excel will be a PHP `DateTime` object.
  113. #### Excel Timestamps
  114. If `RETURNDATE_EXCEL` is set for the Return Date Type, then the returned
  115. date value by any access to the Date and Time functions in Excel will be
  116. a floating point value that represents a number of days from the Excel
  117. base date. The Excel base date is determined by which calendar Excel
  118. uses: the Windows 1900 or the Mac 1904 calendar. 1st January 1900 is the
  119. base date for the Windows 1900 calendar while 1st January 1904 is the
  120. base date for the Mac 1904 calendar.
  121. It is possible for scripts to change the calendar used for calculating
  122. Excel date values by calling the
  123. `\PhpOffice\PhpSpreadsheet\Shared\Date::setExcelCalendar()` method:
  124. ``` php
  125. \PhpOffice\PhpSpreadsheet\Shared\Date::setExcelCalendar($baseDate);
  126. ```
  127. where the following constants can be used for `$baseDate`:
  128. - `\PhpOffice\PhpSpreadsheet\Shared\Date::CALENDAR_WINDOWS_1900`
  129. - `\PhpOffice\PhpSpreadsheet\Shared\Date::CALENDAR_MAC_1904`
  130. The method will return a Boolean True on success, False on failure (e.g.
  131. if an invalid value is passed in).
  132. The `\PhpOffice\PhpSpreadsheet\Shared\Date::getExcelCalendar()` method can
  133. be used to determine the current value of this setting:
  134. ``` php
  135. $baseDate = \PhpOffice\PhpSpreadsheet\Shared\Date::getExcelCalendar();
  136. ```
  137. The default is `CALENDAR_WINDOWS_1900`.
  138. #### Functions that return a Date/Time Value
  139. - DATE
  140. - DATEVALUE
  141. - EDATE
  142. - EOMONTH
  143. - NOW
  144. - TIME
  145. - TIMEVALUE
  146. - TODAY
  147. ### Excel functions that accept Date and Time values as parameters
  148. Date values passed in as parameters to a function can be an Excel
  149. timestamp or a PHP timestamp; or `DateTime` object; or a string containing a
  150. date value (e.g. '1-Jan-2009'). PhpSpreadsheet will attempt to identify
  151. their type based on the PHP datatype:
  152. An integer numeric value will be treated as a PHP/Unix timestamp. A real
  153. (floating point) numeric value will be treated as an Excel
  154. date/timestamp. Any PHP `DateTime` object will be treated as a `DateTime`
  155. object. Any string value (even one containing straight numeric data)
  156. will be converted to a `DateTime` object for validation as a date value
  157. based on the server locale settings, so passing through an ambiguous
  158. value of '07/08/2008' will be treated as 7th August 2008 if your server
  159. settings are UK, but as 8th July 2008 if your server settings are US.
  160. However, if you pass through a value such as '31/12/2008' that would be
  161. considered an error by a US-based server, but which is not ambiguous,
  162. then PhpSpreadsheet will attempt to correct this to 31st December 2008.
  163. If the content of the string doesn’t match any of the formats recognised
  164. by the php `DateTime` object implementation of `strtotime()` (which can
  165. handle a wider range of formats than the normal `strtotime()` function),
  166. then the function will return a `#VALUE` error. However, Excel
  167. recommends that you should always use date/timestamps for your date
  168. functions, and the recommendation for PhpSpreadsheet is the same: avoid
  169. strings because the result is not predictable.
  170. The same principle applies when data is being written to Excel. Cells
  171. containing date actual values (rather than Excel functions that return a
  172. date value) are always written as Excel dates, converting where
  173. necessary. If a cell formatted as a date contains an integer or
  174. `DateTime` object value, then it is converted to an Excel value for
  175. writing: if a cell formatted as a date contains a real value, then no
  176. conversion is required. Note that string values are written as strings
  177. rather than converted to Excel date timestamp values.
  178. #### Functions that expect a Date/Time Value
  179. - DATEDIF
  180. - DAY
  181. - DAYS360
  182. - EDATE
  183. - EOMONTH
  184. - HOUR
  185. - MINUTE
  186. - MONTH
  187. - NETWORKDAYS
  188. - SECOND
  189. - WEEKDAY
  190. - WEEKNUM
  191. - WORKDAY
  192. - YEAR
  193. - YEARFRAC
  194. ### Helper Methods
  195. In addition to the `setExcelCalendar()` and `getExcelCalendar()` methods, a
  196. number of other methods are available in the
  197. `\PhpOffice\PhpSpreadsheet\Shared\Date` class that can help when working
  198. with dates:
  199. #### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($excelDate)
  200. Converts a date/time from an Excel date timestamp to return a PHP
  201. serialized date/timestamp.
  202. Note that this method does not trap for Excel dates that fall outside of
  203. the valid range for a PHP date timestamp.
  204. #### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($excelDate)
  205. Converts a date from an Excel date/timestamp to return a PHP `DateTime`
  206. object.
  207. #### \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($PHPDate)
  208. Converts a PHP serialized date/timestamp or a PHP `DateTime` object to
  209. return an Excel date timestamp.
  210. #### \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($year, $month, $day, $hours=0, $minutes=0, $seconds=0)
  211. Takes year, month and day values (and optional hour, minute and second
  212. values) and returns an Excel date timestamp value.
  213. ### Timezone support for Excel date timestamp conversions
  214. The default timezone for the date functions in PhpSpreadsheet is UST (Universal Standard Time).
  215. If a different timezone needs to be used, these methods are available:
  216. #### \PhpOffice\PhpSpreadsheet\Shared\Date::getDefaultTimezone()
  217. Returns the current timezone value PhpSpeadsheet is using to handle dates and times.
  218. #### \PhpOffice\PhpSpreadsheet\Shared\Date::setDefaultTimezone($timeZone)
  219. Sets the timezone for Excel date timestamp conversions to $timeZone,
  220. which must be a valid PHP DateTimeZone value.
  221. The return value is a Boolean, where true is success,
  222. and false is failure (e.g. an invalid DateTimeZone value was passed.)
  223. #### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($excelDate, $timeZone)
  224. #### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimeStamp($excelDate, $timeZone)
  225. These functions support a timezone as an optional second parameter.
  226. This applies a specific timezone to that function call without affecting the default PhpSpreadsheet Timezone.
  227. ## Function Reference
  228. ### Database Functions
  229. #### DAVERAGE
  230. The DAVERAGE function returns the average value of the cells in a column
  231. of a list or database that match conditions you specify.
  232. ##### Syntax
  233. DAVERAGE (database, field, criteria)
  234. ##### Parameters
  235. **database** The range of cells that makes up the list or database.
  236. A database is a list of related data in which rows of related
  237. information are records, and columns of data are fields. The first row
  238. of the list contains labels for each column.
  239. **field** Indicates which column of the database is used in the
  240. function.
  241. Enter the column label as a string (enclosed between double quotation
  242. marks), such as "Age" or "Yield," or as a number (without quotation
  243. marks) that represents the position of the column within the list: 1 for
  244. the first column, 2 for the second column, and so on.
  245. **criteria** The range of cells that contains the conditions you
  246. specify.
  247. You can use any range for the criteria argument, as long as it includes
  248. at least one column label and at least one cell below the column label
  249. in which you specify a condition for the column.
  250. ##### Return Value
  251. **float** The average value of the matching cells.
  252. This is the statistical mean.
  253. ##### Examples
  254. ``` php
  255. $database = [
  256. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  257. [ 'Apple', 18, 20, 14, 105.00 ],
  258. [ 'Pear', 12, 12, 10, 96.00 ],
  259. [ 'Cherry', 13, 14, 9, 105.00 ],
  260. [ 'Apple', 14, 15, 10, 75.00 ],
  261. [ 'Pear', 9, 8, 8, 76.80 ],
  262. [ 'Apple', 8, 9, 6, 45.00 ],
  263. ];
  264. $criteria = [
  265. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  266. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  267. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  268. ];
  269. $worksheet->fromArray( $criteria, NULL, 'A1' )
  270. ->fromArray( $database, NULL, 'A4' );
  271. $worksheet->setCellValue('A12', '=DAVERAGE(A4:E10,"Yield",A1:B2)');
  272. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  273. // $retVal = 12
  274. ```
  275. ##### Notes
  276. There are no additional notes on this function
  277. #### DCOUNT
  278. The DCOUNT function returns the count of cells that contain a number in
  279. a column of a list or database matching conditions that you specify.
  280. ##### Syntax
  281. DCOUNT(database, [field], criteria)
  282. ##### Parameters
  283. **database** The range of cells that makes up the list or database.
  284. A database is a list of related data in which rows of related
  285. information are records, and columns of data are fields. The first row
  286. of the list contains labels for each column.
  287. **field** Indicates which column of the database is used in the
  288. function.
  289. Enter the column label as a string (enclosed between double quotation
  290. marks), such as "Age" or "Yield," or as a number (without quotation
  291. marks) that represents the position of the column within the list: 1 for
  292. the first column, 2 for the second column, and so on.
  293. **criteria** The range of cells that contains the conditions you
  294. specify.
  295. You can use any range for the criteria argument, as long as it includes
  296. at least one column label and at least one cell below the column label
  297. in which you specify a condition for the column.
  298. ##### Return Value
  299. **float** The count of the matching cells.
  300. ##### Examples
  301. ``` php
  302. $database = [
  303. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  304. [ 'Apple', 18, 20, 14, 105.00 ],
  305. [ 'Pear', 12, 12, 10, 96.00 ],
  306. [ 'Cherry', 13, 14, 9, 105.00 ],
  307. [ 'Apple', 14, 15, 10, 75.00 ],
  308. [ 'Pear', 9, 8, 8, 76.80 ],
  309. [ 'Apple', 8, 9, 6, 45.00 ],
  310. ];
  311. $criteria = [
  312. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  313. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  314. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  315. ];
  316. $worksheet->fromArray( $criteria, NULL, 'A1' )
  317. ->fromArray( $database, NULL, 'A4' );
  318. $worksheet->setCellValue('A12', '=DCOUNT(A4:E10,"Height",A1:B3)');
  319. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  320. // $retVal = 3
  321. ```
  322. ##### Notes
  323. In MS Excel, The field argument is optional. If field is omitted, DCOUNT
  324. counts all records in the database that match the criteria. This logic
  325. has not yet been implemented in PhpSpreadsheet.
  326. #### DCOUNTA
  327. The DCOUNT function returns the count of cells that aren’t blank in a
  328. column of a list or database and that match conditions that you specify.
  329. ##### Syntax
  330. DCOUNTA(database, [field], criteria)
  331. ##### Parameters
  332. **database** The range of cells that makes up the list or database.
  333. A database is a list of related data in which rows of related
  334. information are records, and columns of data are fields. The first row
  335. of the list contains labels for each column.
  336. **field** Indicates which column of the database is used in the
  337. function.
  338. Enter the column label as a string (enclosed between double quotation
  339. marks), such as "Age" or "Yield," or as a number (without quotation
  340. marks) that represents the position of the column within the list: 1 for
  341. the first column, 2 for the second column, and so on.
  342. **criteria** The range of cells that contains the conditions you
  343. specify.
  344. You can use any range for the criteria argument, as long as it includes
  345. at least one column label and at least one cell below the column label
  346. in which you specify a condition for the column.
  347. ##### Return Value
  348. **float** The count of the matching cells.
  349. ##### Examples
  350. ``` php
  351. $database = [
  352. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  353. [ 'Apple', 18, 20, 14, 105.00 ],
  354. [ 'Pear', 12, 12, 10, 96.00 ],
  355. [ 'Cherry', 13, 14, 9, 105.00 ],
  356. [ 'Apple', 14, 15, 10, 75.00 ],
  357. [ 'Pear', 9, 8, 8, 76.80 ],
  358. [ 'Apple', 8, 9, 6, 45.00 ],
  359. ];
  360. $criteria = [
  361. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  362. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  363. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  364. ];
  365. $worksheet->fromArray( $criteria, NULL, 'A1' )
  366. ->fromArray( $database, NULL, 'A4' );
  367. $worksheet->setCellValue('A12', '=DCOUNTA(A4:E10,"Yield",A1:A3)');
  368. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  369. // $retVal = 5
  370. ```
  371. ##### Notes
  372. In MS Excel, The field argument is optional. If field is omitted,
  373. DCOUNTA counts all records in the database that match the criteria. This
  374. logic has not yet been implemented in PhpSpreadsheet.
  375. #### DGET
  376. The DGET function extracts a single value from a column of a list or
  377. database that matches conditions that you specify.
  378. ##### Syntax
  379. DGET(database, field, criteria)
  380. ##### Parameters
  381. **database** The range of cells that makes up the list or database.
  382. A database is a list of related data in which rows of related
  383. information are records, and columns of data are fields. The first row
  384. of the list contains labels for each column.
  385. **field** Indicates which column of the database is used in the
  386. function.
  387. Enter the column label as a string (enclosed between double quotation
  388. marks), such as "Age" or "Yield," or as a number (without quotation
  389. marks) that represents the position of the column within the list: 1 for
  390. the first column, 2 for the second column, and so on.
  391. **criteria** The range of cells that contains the conditions you
  392. specify.
  393. You can use any range for the criteria argument, as long as it includes
  394. at least one column label and at least one cell below the column label
  395. in which you specify a condition for the column.
  396. ##### Return Value
  397. **mixed** The value from the selected column of the matching row.
  398. #### Examples
  399. ``` php
  400. $database = [
  401. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  402. [ 'Apple', 18, 20, 14, 105.00 ],
  403. [ 'Pear', 12, 12, 10, 96.00 ],
  404. [ 'Cherry', 13, 14, 9, 105.00 ],
  405. [ 'Apple', 14, 15, 10, 75.00 ],
  406. [ 'Pear', 9, 8, 8, 76.80 ],
  407. [ 'Apple', 8, 9, 6, 45.00 ],
  408. ];
  409. $criteria = [
  410. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  411. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  412. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  413. ];
  414. $worksheet->fromArray( $criteria, NULL, 'A1' )
  415. ->fromArray( $database, NULL, 'A4' );
  416. $worksheet->setCellValue('A12', '=GET(A4:E10,"Age",A1:F2)');
  417. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  418. // $retVal = 14
  419. ```
  420. ##### Notes
  421. There are no additional notes on this function
  422. #### DMAX
  423. The DMAX function returns the largest number in a column of a list or
  424. database that matches conditions you specify.
  425. ##### Syntax
  426. DMAX(database, field, criteria)
  427. ##### Parameters
  428. **database** The range of cells that makes up the list or database.
  429. A database is a list of related data in which rows of related
  430. information are records, and columns of data are fields. The first row
  431. of the list contains labels for each column.
  432. **field** Indicates which column of the database is used in the
  433. function.
  434. Enter the column label as a string (enclosed between double quotation
  435. marks), such as "Age" or "Yield," or as a number (without quotation
  436. marks) that represents the position of the column within the list: 1 for
  437. the first column, 2 for the second column, and so on.
  438. **criteria** The range of cells that contains the conditions you
  439. specify.
  440. You can use any range for the criteria argument, as long as it includes
  441. at least one column label and at least one cell below the column label
  442. in which you specify a condition for the column.
  443. ##### Return Value
  444. **float** The maximum value of the matching cells.
  445. ##### Examples
  446. ``` php
  447. $database = [
  448. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  449. [ 'Apple', 18, 20, 14, 105.00 ],
  450. [ 'Pear', 12, 12, 10, 96.00 ],
  451. [ 'Cherry', 13, 14, 9, 105.00 ],
  452. [ 'Apple', 14, 15, 10, 75.00 ],
  453. [ 'Pear', 9, 8, 8, 76.80 ],
  454. [ 'Apple', 8, 9, 6, 45.00 ],
  455. ];
  456. $criteria = [
  457. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  458. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  459. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  460. ];
  461. $worksheet->fromArray( $criteria, NULL, 'A1' )
  462. ->fromArray( $database, NULL, 'A4' );
  463. $worksheet->setCellValue('A12', '=DMAX(A4:E10,"Profit",A1:B2)');
  464. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  465. // $retVal = 105
  466. ```
  467. ##### Notes
  468. There are no additional notes on this function
  469. #### DMIN
  470. The DMIN function returns the smallest number in a column of a list or
  471. database that matches conditions you specify.
  472. ##### Syntax
  473. DMIN(database, field, criteria)
  474. ##### Parameters
  475. **database** The range of cells that makes up the list or database.
  476. A database is a list of related data in which rows of related
  477. information are records, and columns of data are fields. The first row
  478. of the list contains labels for each column.
  479. **field** Indicates which column of the database is used in the
  480. function.
  481. Enter the column label as a string (enclosed between double quotation
  482. marks), such as "Age" or "Yield," or as a number (without quotation
  483. marks) that represents the position of the column within the list: 1 for
  484. the first column, 2 for the second column, and so on.
  485. **criteria** The range of cells that contains the conditions you
  486. specify.
  487. You can use any range for the criteria argument, as long as it includes
  488. at least one column label and at least one cell below the column label
  489. in which you specify a condition for the column.
  490. ##### Return Value
  491. **float** The minimum value of the matching cells.
  492. ##### Examples
  493. ``` php
  494. $database = [
  495. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  496. [ 'Apple', 18, 20, 14, 105.00 ],
  497. [ 'Pear', 12, 12, 10, 96.00 ],
  498. [ 'Cherry', 13, 14, 9, 105.00 ],
  499. [ 'Apple', 14, 15, 10, 75.00 ],
  500. [ 'Pear', 9, 8, 8, 76.80 ],
  501. [ 'Apple', 8, 9, 6, 45.00 ],
  502. ];
  503. $criteria = [
  504. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  505. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  506. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  507. ];
  508. $worksheet->fromArray( $criteria, NULL, 'A1' )
  509. ->fromArray( $database, NULL, 'A4' );
  510. $worksheet->setCellValue('A12', '=DMIN(A4:E10,"Yield",A1:A3)');
  511. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  512. // $retVal = 6
  513. ```
  514. ##### Notes
  515. There are no additional notes on this function
  516. #### DPRODUCT
  517. The DPRODUCT function multiplies the values in a column of a list or
  518. database that match conditions that you specify.
  519. ##### Syntax
  520. DPRODUCT(database, field, criteria)
  521. ##### Parameters
  522. **database** The range of cells that makes up the list or database.
  523. A database is a list of related data in which rows of related
  524. information are records, and columns of data are fields. The first row
  525. of the list contains labels for each column.
  526. **field** Indicates which column of the database is used in the
  527. function.
  528. Enter the column label as a string (enclosed between double quotation
  529. marks), such as "Age" or "Yield," or as a number (without quotation
  530. marks) that represents the position of the column within the list: 1 for
  531. the first column, 2 for the second column, and so on.
  532. **criteria** The range of cells that contains the conditions you
  533. specify.
  534. You can use any range for the criteria argument, as long as it includes
  535. at least one column label and at least one cell below the column label
  536. in which you specify a condition for the column.
  537. ##### Return Value
  538. **float** The product of the matching cells.
  539. ##### Examples
  540. ``` php
  541. $database = [
  542. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  543. [ 'Apple', 18, 20, 14, 105.00 ],
  544. [ 'Pear', 12, 12, 10, 96.00 ],
  545. [ 'Cherry', 13, 14, 9, 105.00 ],
  546. [ 'Apple', 14, 15, 10, 75.00 ],
  547. [ 'Pear', 9, 8, 8, 76.80 ],
  548. [ 'Apple', 8, 9, 6, 45.00 ],
  549. ];
  550. $criteria = [
  551. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  552. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  553. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  554. ];
  555. $worksheet->fromArray( $criteria, NULL, 'A1' )
  556. ->fromArray( $database, NULL, 'A4' );
  557. $worksheet->setCellValue('A12', '=DPRODUCT(A4:E10,"Yield",A1:B2)');
  558. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  559. // $retVal = 140
  560. ```
  561. ##### Notes
  562. There are no additional notes on this function
  563. #### DSTDEV
  564. The DSTDEV function estimates the standard deviation of a population
  565. based on a sample by using the numbers in a column of a list or database
  566. that match conditions that you specify.
  567. ##### Syntax
  568. DSTDEV(database, field, criteria)
  569. ##### Parameters
  570. **database** The range of cells that makes up the list or database.
  571. A database is a list of related data in which rows of related
  572. information are records, and columns of data are fields. The first row
  573. of the list contains labels for each column.
  574. **field** Indicates which column of the database is used in the
  575. function.
  576. Enter the column label as a string (enclosed between double quotation
  577. marks), such as "Age" or "Yield," or as a number (without quotation
  578. marks) that represents the position of the column within the list: 1 for
  579. the first column, 2 for the second column, and so on.
  580. **criteria** The range of cells that contains the conditions you
  581. specify.
  582. You can use any range for the criteria argument, as long as it includes
  583. at least one column label and at least one cell below the column label
  584. in which you specify a condition for the column.
  585. ##### Return Value
  586. **float** The estimated standard deviation of the matching cells.
  587. ##### Examples
  588. ``` php
  589. $database = [
  590. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  591. [ 'Apple', 18, 20, 14, 105.00 ],
  592. [ 'Pear', 12, 12, 10, 96.00 ],
  593. [ 'Cherry', 13, 14, 9, 105.00 ],
  594. [ 'Apple', 14, 15, 10, 75.00 ],
  595. [ 'Pear', 9, 8, 8, 76.80 ],
  596. [ 'Apple', 8, 9, 6, 45.00 ],
  597. ];
  598. $criteria = [
  599. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  600. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  601. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  602. ];
  603. $worksheet->fromArray( $criteria, NULL, 'A1' )
  604. ->fromArray( $database, NULL, 'A4' );
  605. $worksheet->setCellValue('A12', '=DSTDEV(A4:E10,"Yield",A1:A3)');
  606. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  607. // $retVal = 2.97
  608. ```
  609. ##### Notes
  610. There are no additional notes on this function
  611. #### DSTDEVP
  612. The DSTDEVP function calculates the standard deviation of a population
  613. based on the entire population by using the numbers in a column of a
  614. list or database that match conditions that you specify.
  615. ##### Syntax
  616. DSTDEVP(database, field, criteria)
  617. ##### Parameters
  618. **database** The range of cells that makes up the list or database.
  619. A database is a list of related data in which rows of related
  620. information are records, and columns of data are fields. The first row
  621. of the list contains labels for each column.
  622. **field** Indicates which column of the database is used in the
  623. function.
  624. Enter the column label as a string (enclosed between double quotation
  625. marks), such as "Age" or "Yield," or as a number (without quotation
  626. marks) that represents the position of the column within the list: 1 for
  627. the first column, 2 for the second column, and so on.
  628. **criteria** The range of cells that contains the conditions you
  629. specify.
  630. You can use any range for the criteria argument, as long as it includes
  631. at least one column label and at least one cell below the column label
  632. in which you specify a condition for the column.
  633. ##### Return Value
  634. **float** The estimated standard deviation of the matching cells.
  635. ##### Examples
  636. ``` php
  637. $database = [
  638. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  639. [ 'Apple', 18, 20, 14, 105.00 ],
  640. [ 'Pear', 12, 12, 10, 96.00 ],
  641. [ 'Cherry', 13, 14, 9, 105.00 ],
  642. [ 'Apple', 14, 15, 10, 75.00 ],
  643. [ 'Pear', 9, 8, 8, 76.80 ],
  644. [ 'Apple', 8, 9, 6, 45.00 ],
  645. ];
  646. $criteria = [
  647. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  648. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  649. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  650. ];
  651. $worksheet->fromArray( $criteria, NULL, 'A1' )
  652. ->fromArray( $database, NULL, 'A4' );
  653. $worksheet->setCellValue('A12', '=DSTDEVP(A4:E10,"Yield",A1:A3)');
  654. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  655. // $retVal = 2.65
  656. ```
  657. ##### Notes
  658. There are no additional notes on this function
  659. #### DSUM
  660. The DSUM function adds the numbers in a column of a list or database
  661. that matches conditions you specify.
  662. ##### Syntax
  663. DSUM(database, field, criteria)
  664. ##### Parameters
  665. **database** The range of cells that makes up the list or database.
  666. A database is a list of related data in which rows of related
  667. information are records, and columns of data are fields. The first row
  668. of the list contains labels for each column.
  669. **field** Indicates which column of the database is used in the
  670. function.
  671. Enter the column label as a string (enclosed between double quotation
  672. marks), such as "Age" or "Yield," or as a number (without quotation
  673. marks) that represents the position of the column within the list: 1 for
  674. the first column, 2 for the second column, and so on.
  675. **criteria** The range of cells that contains the conditions you
  676. specify.
  677. You can use any range for the criteria argument, as long as it includes
  678. at least one column label and at least one cell below the column label
  679. in which you specify a condition for the column.
  680. ##### Return Value
  681. **float** The total value of the matching cells.
  682. ##### Examples
  683. ``` php
  684. $database = [
  685. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ],
  686. [ 'Apple', 18, 20, 14, 105.00 ],
  687. [ 'Pear', 12, 12, 10, 96.00 ],
  688. [ 'Cherry', 13, 14, 9, 105.00 ],
  689. [ 'Apple', 14, 15, 10, 75.00 ],
  690. [ 'Pear', 9, 8, 8, 76.80 ],
  691. [ 'Apple', 8, 9, 6, 45.00 ],
  692. ];
  693. $criteria = [
  694. [ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ],
  695. [ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ],
  696. [ '="=Pear"', NULL, NULL, NULL, NULL, NULL ],
  697. ];
  698. $worksheet->fromArray( $criteria, NULL, 'A1' )
  699. ->fromArray( $database, NULL, 'A4' );
  700. $worksheet->setCellValue('A12', '=DMIN(A4:E10,"Profit",A1:A2)');
  701. $retVal = $worksheet->getCell('A12')->getCalculatedValue();
  702. // $retVal = 225
  703. ```
  704. ##### Notes
  705. There are no additional notes on this function
  706. #### DVAR
  707. Not yet documented.
  708. #### DVARP
  709. Not yet documented.
  710. ### Date and Time Functions
  711. Excel provides a number of functions for the manipulation of dates and
  712. times, and calculations based on date/time values. it is worth spending
  713. some time reading the section titled "Date and Time Values" on passing
  714. date parameters and returning date values to understand how
  715. PhpSpreadsheet reconciles the differences between dates and times in
  716. Excel and in PHP.
  717. #### DATE
  718. The DATE function returns an Excel timestamp or a PHP timestamp or `DateTime`
  719. object representing the date that is referenced by the parameters.
  720. ##### Syntax
  721. DATE(year, month, day)
  722. ##### Parameters
  723. **year** The year number.
  724. If this value is between 0 (zero) and 1899 inclusive (for the Windows
  725. 1900 calendar), or between 4 and 1903 inclusive (for the Mac 1904), then
  726. PhpSpreadsheet adds it to the Calendar base year, so a value of 108 will
  727. interpret the year as 2008 when using the Windows 1900 calendar, or 2012
  728. when using the Mac 1904 calendar.
  729. **month** The month number.
  730. If this value is greater than 12, the DATE function adds that number of
  731. months to the first month in the year specified. For example,
  732. DATE(2008,14,2) returns a value representing February 2, 2009.
  733. If the value of **month** is less than 1, then that value will be
  734. adjusted by -1, and that will then be subtracted from the first month of
  735. the year specified. For example, DATE(2008,0,2) returns a value
  736. representing December 2, 2007; while DATE(2008,-1,2) returns a value
  737. representing November 2, 2007.
  738. **day** The day number.
  739. If this value is greater than the number of days in the month (and year)
  740. specified, the DATE function adds that number of days to the first day
  741. in the month. For example, DATE(2008,1,35) returns a value representing
  742. February 4, 2008.
  743. If the value of **day** is less than 1, then that value will be adjusted
  744. by -1, and that will then be subtracted from the first month of the year
  745. specified. For example, DATE(2008,3,0) returns a value representing
  746. February 29, 2008; while DATE(2008,3,-2) returns a value representing
  747. February 27, 2008.
  748. ##### Return Value
  749. **mixed** A date/time stamp that corresponds to the given date.
  750. This could be a PHP timestamp value (integer), a PHP `DateTime` object,
  751. or an Excel timestamp value (real), depending on the value of
  752. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`.
  753. ##### Examples
  754. ``` php
  755. $worksheet->setCellValue('A1', 'Year')
  756. ->setCellValue('A2', 'Month')
  757. ->setCellValue('A3', 'Day');
  758. $worksheet->setCellValue('B1', 2008)
  759. ->setCellValue('B2', 12)
  760. ->setCellValue('B3', 31);
  761. $worksheet->setCellValue('D1', '=DATE(B1,B2,B3)');
  762. $retVal = $worksheet->getCell('D1')->getCalculatedValue();
  763. // $retVal = 1230681600
  764. ```
  765. ``` php
  766. // We're going to be calling the same cell calculation multiple times,
  767. // and expecting different return values, so disable calculation cacheing
  768. \PhpOffice\PhpSpreadsheet\Calculation\Calculation::getInstance()->setCalculationCacheEnabled(FALSE);
  769. $saveFormat = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType();
  770. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  771. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
  772. );
  773. $retVal = call_user_func_array(
  774. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATE'],
  775. [2008, 12, 31]
  776. );
  777. // $retVal = 39813.0
  778. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  779. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC
  780. );
  781. $retVal = call_user_func_array(
  782. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATE'],
  783. [2008, 12, 31]
  784. );
  785. // $retVal = 1230681600
  786. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($saveFormat);
  787. ```
  788. ##### Notes
  789. There are no additional notes on this function
  790. #### DATEDIF
  791. The DATEDIF function computes the difference between two dates in a
  792. variety of different intervals, such number of years, months, or days.
  793. ##### Syntax
  794. DATEDIF(date1, date2 [, unit])
  795. ##### Parameters
  796. **date1** First Date.
  797. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  798. represented as a string.
  799. **date2** Second Date.
  800. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  801. represented as a string.
  802. **unit** The interval type to use for the calculation
  803. This is a string, comprising one of the values listed below:
  804. Unit | Meaning | Description
  805. -----|---------------------------------|--------------------------------
  806. m | Months | Complete calendar months between the dates.
  807. d | Days | Number of days between the dates.
  808. y | Years | Complete calendar years between the dates.
  809. ym | Months Excluding Years | Complete calendar months between the dates as if they were of the same year.
  810. yd | Days Excluding Years | Complete calendar days between the dates as if they were of the same year.
  811. md | Days Excluding Years And Months | Complete calendar days between the dates as if they were of the same month and same year.
  812. The unit value is not case sensitive, and defaults to `d`.
  813. ##### Return Value
  814. **integer** An integer value that reflects the difference between the
  815. two dates.
  816. This could be the number of full days, months or years between the two
  817. dates, depending on the interval unit value passed into the function as
  818. the third parameter.
  819. ##### Examples
  820. ``` php
  821. $worksheet->setCellValue('A1', 'Year')
  822. ->setCellValue('A2', 'Month')
  823. ->setCellValue('A3', 'Day');
  824. $worksheet->setCellValue('B1', 2001)
  825. ->setCellValue('C1', 2009)
  826. ->setCellValue('B2', 7)
  827. ->setCellValue('C2', 12)
  828. ->setCellValue('B3', 1)
  829. ->setCellValue('C3', 31);
  830. $worksheet->setCellValue('D1', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"d")')
  831. ->setCellValue('D2', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"m")')
  832. ->setCellValue('D3', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"y")')
  833. ->setCellValue('D4', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"ym")')
  834. ->setCellValue('D5', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"yd")')
  835. ->setCellValue('D6', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"md")');
  836. $retVal = $worksheet->getCell('D1')->getCalculatedValue();
  837. // $retVal = 3105
  838. $retVal = $worksheet->getCell('D2')->getCalculatedValue();
  839. // $retVal = 101
  840. $retVal = $worksheet->getCell('D3')->getCalculatedValue();
  841. // $retVal = 8
  842. $retVal = $worksheet->getCell('D4')->getCalculatedValue();
  843. // $retVal = 5
  844. $retVal = $worksheet->getCell('D5')->getCalculatedValue();
  845. // $retVal = 183
  846. $retVal = $worksheet->getCell('D6')->getCalculatedValue();
  847. // $retVal = 30
  848. ```
  849. ``` php
  850. $date1 = 1193317015; // PHP timestamp for 25-Oct-2007
  851. $date2 = 1449579415; // PHP timestamp for 8-Dec-2015
  852. $retVal = call_user_func_array(
  853. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
  854. [$date1, $date2, 'd']
  855. );
  856. // $retVal = 2966
  857. $retVal = call_user_func_array(
  858. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
  859. [$date1, $date2, 'm']
  860. );
  861. // $retVal = 97
  862. $retVal = call_user_func_array(
  863. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
  864. [$date1, $date2, 'y']
  865. );
  866. // $retVal = 8
  867. $retVal = call_user_func_array(
  868. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
  869. [$date1, $date2, 'ym']
  870. );
  871. // $retVal = 1
  872. $retVal = call_user_func_array(
  873. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
  874. [$date1, $date2, 'yd']
  875. );
  876. // $retVal = 44
  877. $retVal = call_user_func_array(
  878. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
  879. [$date1, $date2, 'md']
  880. );
  881. // $retVal = 13
  882. ```
  883. ##### Notes
  884. If Date1 is later than Date2, DATEDIF will return a \#NUM! error.
  885. #### DATEVALUE
  886. The DATEVALUE function returns the date represented by a date formatted
  887. as a text string. Use DATEVALUE to convert a date represented by text to
  888. a serial number.
  889. ##### Syntax
  890. DATEVALUE(dateString)
  891. ##### Parameters
  892. **date** Date String.
  893. A string, representing a date value.
  894. ##### Return Value
  895. **mixed** A date/time stamp that corresponds to the given date.
  896. This could be a PHP timestamp value (integer), a PHP `DateTime` object,
  897. or an Excel timestamp value (real), depending on the value of
  898. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`.
  899. ##### Examples
  900. ``` php
  901. $worksheet->setCellValue('A1', 'Date String');
  902. ->setCellValue('A2', '31-Dec-2008')
  903. ->setCellValue('A3', '31/12/2008')
  904. ->setCellValue('A4', '12-31-2008');
  905. $worksheet->setCellValue('B2', '=DATEVALUE(A2)')
  906. ->setCellValue('B3', '=DATEVALUE(A3)')
  907. ->setCellValue('B4', '=DATEVALUE(A4)');
  908. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  909. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
  910. );
  911. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  912. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  913. $retVal = $worksheet->getCell('B4')->getCalculatedValue();
  914. // $retVal = 39813.0 for all cases
  915. ```
  916. ``` php
  917. // We're going to be calling the same cell calculation multiple times,
  918. // and expecting different return values, so disable calculation cacheing
  919. \PhpOffice\PhpSpreadsheet\Calculation\Calculation::getInstance()->setCalculationCacheEnabled(FALSE);
  920. $saveFormat = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType();
  921. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  922. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
  923. );
  924. $retVal = call_user_func_array(
  925. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEVALUE'],
  926. ['31-Dec-2008']
  927. );
  928. // $retVal = 39813.0
  929. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  930. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC
  931. );
  932. $retVal = call_user_func_array(
  933. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEVALUE'],
  934. ['31-Dec-2008']
  935. );
  936. // $retVal = 1230681600
  937. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($saveFormat);
  938. ```
  939. ##### Notes
  940. DATEVALUE uses the php `DateTime` object implementation of `strtotime()`
  941. (which can handle a wider range of formats than the normal `strtotime()`
  942. function), and it is also called for any date parameter passed to other
  943. date functions (such as DATEDIF) when the parameter value is a string.
  944. **WARNING:-** PhpSpreadsheet accepts a wider range of date formats than
  945. MS Excel, so it is entirely possible that Excel will return a \#VALUE!
  946. error when passed a date string that it can’t interpret, while
  947. PhpSpreadsheet is able to translate that same string into a correct date
  948. value.
  949. Care should be taken in workbooks that use string formatted dates in
  950. calculations when writing to Xls or Xlsx.
  951. #### DAY
  952. The DAY function returns the day of a date. The day is given as an
  953. integer ranging from 1 to 31.
  954. ##### Syntax
  955. DAY(datetime)
  956. ##### Parameters
  957. **datetime** Date.
  958. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  959. represented as a string.
  960. ##### Return Value
  961. **integer** An integer value that reflects the day of the month.
  962. This is an integer ranging from 1 to 31.
  963. ##### Examples
  964. ``` php
  965. $worksheet->setCellValue('A1', 'Date String')
  966. ->setCellValue('A2', '31-Dec-2008')
  967. ->setCellValue('A3', '14-Feb-2008');
  968. $worksheet->setCellValue('B2', '=DAY(A2)')
  969. ->setCellValue('B3', '=DAY(A3)');
  970. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  971. // $retVal = 31
  972. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  973. // $retVal = 14
  974. ```
  975. ``` php
  976. $retVal = call_user_func_array(
  977. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYOFMONTH'],
  978. ['25-Dec-2008']
  979. );
  980. // $retVal = 25
  981. ```
  982. ##### Notes
  983. Note that the PhpSpreadsheet function is
  984. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::DAYOFMONTH()` when the
  985. method is called statically.
  986. #### DAYS360
  987. The DAYS360 function computes the difference between two dates based on
  988. a 360 day year (12 equal periods of 30 days each) used by some
  989. accounting systems.
  990. ##### Syntax
  991. DAYS360(date1, date2 [, method])
  992. #### Parameters
  993. **date1** First Date.
  994. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  995. represented as a string.
  996. **date2** Second Date.
  997. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  998. represented as a string.
  999. **method** A boolean flag (TRUE or FALSE)
  1000. This is a flag that determines which method to use in the calculation,
  1001. based on the values listed below:
  1002. method | Description
  1003. -------|------------
  1004. FALSE | U.S. (NASD) method. If the starting date is the last day of a month, it becomes equal to the 30th of the same month. If the ending date is the last day of a month and the starting date is earlier than the 30th of a month, the ending date becomes equal to the 1st of the next month; otherwise the ending date becomes equal to the 30th of the same month.
  1005. TRUE | European method. Starting dates and ending dates that occur on the 31st of a month become equal to the 30th of the same month.
  1006. The method value defaults to FALSE.
  1007. ##### Return Value
  1008. **integer** An integer value that reflects the difference between the
  1009. two dates.
  1010. This is the number of full days between the two dates, based on a 360
  1011. day year.
  1012. ##### Examples
  1013. ``` php
  1014. $worksheet->setCellValue('B1', 'Start Date')
  1015. ->setCellValue('C1', 'End Date')
  1016. ->setCellValue('A2', 'Year')
  1017. ->setCellValue('A3', 'Month')
  1018. ->setCellValue('A4', 'Day');
  1019. $worksheet->setCellValue('B2', 2003)
  1020. ->setCellValue('B3', 2)
  1021. ->setCellValue('B4', 3);
  1022. $worksheet->setCellValue('C2', 2007)
  1023. ->setCellValue('C3', 5)
  1024. ->setCellValue('C4', 31);
  1025. $worksheet->setCellValue('E2', '=DAYS360(DATE(B2,B3,B4),DATE(C2,C3,C4))')
  1026. ->setCellValue('E4', '=DAYS360(DATE(B2,B3,B4),DATE(C2,C3,C4),FALSE)');
  1027. $retVal = $worksheet->getCell('E2')->getCalculatedValue();
  1028. // $retVal = 1558
  1029. $retVal = $worksheet->getCell('E4')->getCalculatedValue();
  1030. // $retVal = 1557
  1031. ```
  1032. ``` php
  1033. $date1 = 37655.0; // Excel timestamp for 25-Oct-2007
  1034. $date2 = 39233.0; // Excel timestamp for 8-Dec-2015
  1035. $retVal = call_user_func_array(
  1036. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYS360'],
  1037. [$date1, $date2]
  1038. );
  1039. // $retVal = 1558
  1040. $retVal = call_user_func_array(
  1041. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYS360'],
  1042. [$date1, $date2, TRUE]
  1043. );
  1044. // $retVal = 1557
  1045. ```
  1046. ##### Notes
  1047. **WARNING:-** This function does not currently work with the Xls Writer
  1048. when a PHP Boolean is used for the third (optional) parameter (as shown
  1049. in the example above), and the writer will generate and error. It will
  1050. work if a numeric 0 or 1 is used for the method parameter; or if the
  1051. Excel `TRUE()` and `FALSE()` functions are used instead.
  1052. #### EDATE
  1053. The EDATE function returns an Excel timestamp or a PHP timestamp or `DateTime`
  1054. object representing the date that is the indicated number of months
  1055. before or after a specified date (the start\_date). Use EDATE to
  1056. calculate maturity dates or due dates that fall on the same day of the
  1057. month as the date of issue.
  1058. ##### Syntax
  1059. EDATE(baseDate, months)
  1060. ##### Parameters
  1061. **baseDate** Start Date.
  1062. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  1063. represented as a string.
  1064. **months** Number of months to add.
  1065. An integer value indicating the number of months before or after
  1066. baseDate. A positive value for months yields a future date; a negative
  1067. value yields a past date.
  1068. ##### Return Value
  1069. **mixed** A date/time stamp that corresponds to the basedate + months.
  1070. This could be a PHP timestamp value (integer), a PHP `DateTime` object,
  1071. or an Excel timestamp value (real), depending on the value of
  1072. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`.
  1073. ##### Examples
  1074. ``` php
  1075. $worksheet->setCellValue('A1', 'Date String')
  1076. ->setCellValue('A2', '1-Jan-2008')
  1077. ->setCellValue('A3', '29-Feb-2008');
  1078. $worksheet->setCellValue('B2', '=EDATE(A2,5)')
  1079. ->setCellValue('B3', '=EDATE(A3,-12)');
  1080. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  1081. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
  1082. );
  1083. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1084. // $retVal = 39600.0 (1-Jun-2008)
  1085. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1086. // $retVal = 39141.0 (28-Feb-2007)
  1087. ```
  1088. ``` php
  1089. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  1090. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
  1091. );
  1092. $retVal = call_user_func_array(
  1093. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'EDATE'],
  1094. ['31-Oct-2008', 25]
  1095. );
  1096. // $retVal = 40512.0 (30-Nov-2010)
  1097. ```
  1098. ###### Notes
  1099. **WARNING:-** This function is currently not supported by the Xls Writer
  1100. because it is not a standard function within Excel 5, but an add-in from
  1101. the Analysis ToolPak.
  1102. #### EOMONTH
  1103. The EOMONTH function returns an Excel timestamp or a PHP timestamp or
  1104. `DateTime` object representing the date of the last day of the month that is
  1105. the indicated number of months before or after a specified date (the
  1106. start\_date). Use EOMONTH to calculate maturity dates or due dates that
  1107. fall on the last day of the month.
  1108. ##### Syntax
  1109. EOMONTH(baseDate, months)
  1110. ##### Parameters
  1111. **baseDate** Start Date.
  1112. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  1113. represented as a string.
  1114. **months** Number of months to add.
  1115. An integer value indicating the number of months before or after
  1116. baseDate. A positive value for months yields a future date; a negative
  1117. value yields a past date.
  1118. ##### Return Value
  1119. **mixed** A date/time stamp that corresponds to the last day of basedate
  1120. + months.
  1121. This could be a PHP timestamp value (integer), a PHP `DateTime` object,
  1122. or an Excel timestamp value (real), depending on the value of
  1123. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`.
  1124. ##### Examples
  1125. ``` php
  1126. $worksheet->setCellValue('A1', 'Date String')
  1127. ->setCellValue('A2', '1-Jan-2000')
  1128. ->setCellValue('A3', '14-Feb-2009');
  1129. $worksheet->setCellValue('B2', '=EOMONTH(A2,5)')
  1130. ->setCellValue('B3', '=EOMONTH(A3,-12)');
  1131. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL);
  1132. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1133. // $retVal = 39629.0 (30-Jun-2008)
  1134. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1135. // $retVal = 39507.0 (29-Feb-2008)
  1136. ```
  1137. ``` php
  1138. \PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
  1139. \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
  1140. );
  1141. $retVal = call_user_func_array(
  1142. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'EOMONTH'],
  1143. ['31-Oct-2008', 13]
  1144. );
  1145. // $retVal = 40147.0 (30-Nov-2010)
  1146. ```
  1147. ##### Notes
  1148. **WARNING:-** This function is currently not supported by the Xls Writer
  1149. because it is not a standard function within Excel 5, but an add-in from
  1150. the Analysis ToolPak.
  1151. #### HOUR
  1152. The HOUR function returns the hour of a time value. The hour is given as
  1153. an integer, ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.).
  1154. ##### Syntax
  1155. HOUR(datetime)
  1156. ##### Parameters
  1157. **datetime** Time.
  1158. An Excel date/time value, PHP date timestamp, PHP `DateTime` object, or a
  1159. date/time represented as a string.
  1160. ##### Return Value
  1161. **integer** An integer value that reflects the hour of the day.
  1162. This is an integer ranging from 0 to 23.
  1163. ##### Examples
  1164. ``` php
  1165. $worksheet->setCellValue('A1', 'Time String')
  1166. ->setCellValue('A2', '31-Dec-2008 17:30')
  1167. ->setCellValue('A3', '14-Feb-2008 4:20 AM')
  1168. ->setCellValue('A4', '14-Feb-2008 4:20 PM');
  1169. $worksheet->setCellValue('B2', '=HOUR(A2)')
  1170. ->setCellValue('B3', '=HOUR(A3)')
  1171. ->setCellValue('B4', '=HOUR(A4)');
  1172. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1173. // $retVal = 17
  1174. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1175. // $retVal = 4
  1176. $retVal = $worksheet->getCell('B4')->getCalculatedValue();
  1177. // $retVal = 16
  1178. ```
  1179. ``` php
  1180. $retVal = call_user_func_array(
  1181. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'HOUROFDAY'],
  1182. ['09:30']
  1183. );
  1184. // $retVal = 9
  1185. ```
  1186. ##### Notes
  1187. Note that the PhpSpreadsheet function is
  1188. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::HOUROFDAY()` when the
  1189. method is called statically.
  1190. #### MINUTE
  1191. The MINUTE function returns the minutes of a time value. The minute is
  1192. given as an integer, ranging from 0 to 59.
  1193. ##### Syntax
  1194. MINUTE(datetime)
  1195. ##### Parameters
  1196. **datetime** Time.
  1197. An Excel date/time value, PHP date timestamp, PHP `DateTime` object, or a
  1198. date/time represented as a string.
  1199. ##### Return Value
  1200. **integer** An integer value that reflects the minutes within the hour.
  1201. This is an integer ranging from 0 to 59.
  1202. ##### Examples
  1203. ``` php
  1204. $worksheet->setCellValue('A1', 'Time String')
  1205. ->setCellValue('A2', '31-Dec-2008 17:30')
  1206. ->setCellValue('A3', '14-Feb-2008 4:20 AM')
  1207. ->setCellValue('A4', '14-Feb-2008 4:45 PM');
  1208. $worksheet->setCellValue('B2', '=MINUTE(A2)')
  1209. ->setCellValue('B3', '=MINUTE(A3)')
  1210. ->setCellValue('B4', '=MINUTE(A4)');
  1211. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1212. // $retVal = 30
  1213. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1214. // $retVal = 20
  1215. $retVal = $worksheet->getCell('B4')->getCalculatedValue();
  1216. // $retVal = 45
  1217. ```
  1218. ``` php
  1219. $retVal = call_user_func_array(
  1220. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'MINUTE'],
  1221. ['09:30']
  1222. );
  1223. // $retVal = 30
  1224. ```
  1225. ##### Notes
  1226. Note that the PhpSpreadsheet function is
  1227. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::MINUTE()` when the
  1228. method is called statically.
  1229. #### MONTH
  1230. The MONTH function returns the month of a date. The month is given as an
  1231. integer ranging from 1 to 12.
  1232. ##### Syntax
  1233. MONTH(datetime)
  1234. ##### Parameters
  1235. **datetime** Date.
  1236. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  1237. represented as a string.
  1238. ##### Return Value
  1239. **integer** An integer value that reflects the month of the year.
  1240. This is an integer ranging from 1 to 12.
  1241. ##### Examples
  1242. ``` php
  1243. $worksheet->setCellValue('A1', 'Date String');
  1244. $worksheet->setCellValue('A2', '31-Dec-2008');
  1245. $worksheet->setCellValue('A3', '14-Feb-2008');
  1246. $worksheet->setCellValue('B2', '=MONTH(A2)');
  1247. $worksheet->setCellValue('B3', '=MONTH(A3)');
  1248. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1249. // $retVal = 12
  1250. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1251. // $retVal = 2
  1252. ```
  1253. ``` php
  1254. $retVal = call_user_func_array(
  1255. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'MONTHOFYEAR'],
  1256. ['14-July-2008']
  1257. );
  1258. // $retVal = 7
  1259. ```
  1260. #### Notes
  1261. Note that the PhpSpreadsheet function is
  1262. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::MONTHOFYEAR()` when the
  1263. method is called statically.
  1264. #### NETWORKDAYS
  1265. The NETWORKDAYS function returns the number of whole working days
  1266. between a *start date* and an *end date*. Working days exclude weekends
  1267. and any dates identified in *holidays*. Use NETWORKDAYS to calculate
  1268. employee benefits that accrue based on the number of days worked during
  1269. a specific term.
  1270. ##### Syntax
  1271. NETWORKDAYS(startDate, endDate [, holidays])
  1272. ##### Parameters
  1273. **startDate** Start Date of the period.
  1274. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  1275. represented as a string.
  1276. **endDate** End Date of the period.
  1277. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  1278. represented as a string.
  1279. **holidays** Optional array of Holiday dates.
  1280. An optional range of one or more dates to exclude from the working
  1281. calendar, such as state and federal holidays and floating holidays.
  1282. The list can be either a range of cells that contains the dates or an
  1283. array constant of Excel date values, PHP date timestamps, PHP date
  1284. objects, or dates represented as strings.
  1285. ##### Return Value
  1286. **integer** Number of working days.
  1287. The number of working days between startDate and endDate.
  1288. ##### Examples
  1289. ``` php
  1290. ```
  1291. ``` php
  1292. ```
  1293. ##### Notes
  1294. There are no additional notes on this function
  1295. #### NOW
  1296. The NOW function returns the current date and time.
  1297. ##### Syntax
  1298. NOW()
  1299. ##### Parameters
  1300. There are no parameters for the `NOW()` function.
  1301. ##### Return Value
  1302. **mixed** A date/time stamp that corresponds to the current date and
  1303. time.
  1304. This could be a PHP timestamp value (integer), a PHP `DateTime` object,
  1305. or an Excel timestamp value (real), depending on the value of
  1306. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`.
  1307. ##### Examples
  1308. ``` php
  1309. ```
  1310. ``` php
  1311. ```
  1312. ##### Notes
  1313. Note that the PhpSpreadsheet function is
  1314. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::DATETIMENOW()` when the
  1315. method is called statically.
  1316. #### SECOND
  1317. The SECOND function returns the seconds of a time value. The second is
  1318. given as an integer, ranging from 0 to 59.
  1319. ##### Syntax
  1320. SECOND(datetime)
  1321. ##### Parameters
  1322. **datetime** Time.
  1323. An Excel date/time value, PHP date timestamp, PHP `DateTime` object, or a
  1324. date/time represented as a string.
  1325. ##### Return Value
  1326. **integer** An integer value that reflects the seconds within the
  1327. minute.
  1328. This is an integer ranging from 0 to 59.
  1329. ##### Examples
  1330. ``` php
  1331. $worksheet->setCellValue('A1', 'Time String')
  1332. ->setCellValue('A2', '31-Dec-2008 17:30:20')
  1333. ->setCellValue('A3', '14-Feb-2008 4:20 AM')
  1334. ->setCellValue('A4', '14-Feb-2008 4:45:59 PM');
  1335. $worksheet->setCellValue('B2', '=SECOND(A2)')
  1336. ->setCellValue('B3', '=SECOND(A3)');
  1337. ->setCellValue('B4', '=SECOND(A4)');
  1338. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1339. // $retVal = 20
  1340. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1341. // $retVal = 0
  1342. $retVal = $worksheet->getCell('B4')->getCalculatedValue();
  1343. // $retVal = 59
  1344. ```
  1345. ``` php
  1346. $retVal = call_user_func_array(
  1347. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'SECOND'],
  1348. ['09:30:17']
  1349. );
  1350. // $retVal = 17
  1351. ```
  1352. ##### Notes
  1353. Note that the PhpSpreadsheet function is
  1354. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::SECOND()` when the
  1355. method is called statically.
  1356. #### TIME
  1357. Not yet documented.
  1358. #### TIMEVALUE
  1359. Not yet documented.
  1360. #### TODAY
  1361. Not yet documented.
  1362. #### WEEKDAY
  1363. The WEEKDAY function returns the day of the week for a given date. The
  1364. day is given as an integer ranging from 1 to 7, although this can be
  1365. modified to return a value between 0 and 6.
  1366. ##### Syntax
  1367. WEEKDAY(datetime [, method])
  1368. ##### Parameters
  1369. **datetime** Date.
  1370. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  1371. represented as a string.
  1372. **method** An integer flag (values 0, 1 or 2)
  1373. This is a flag that determines which method to use in the calculation,
  1374. based on the values listed below:
  1375. method | Description
  1376. :-----:|------------------------------------------
  1377. 0 | Returns 1 (Sunday) through 7 (Saturday).
  1378. 1 | Returns 1 (Monday) through 7 (Sunday).
  1379. 2 | Returns 0 (Monday) through 6 (Sunday).
  1380. The method value defaults to 1.
  1381. ##### Return Value
  1382. **integer** An integer value that reflects the day of the week.
  1383. This is an integer ranging from 1 to 7, or 0 to 6, depending on the
  1384. value of method.
  1385. ##### Examples
  1386. ``` php
  1387. $worksheet->setCellValue('A1', 'Date String')
  1388. ->setCellValue('A2', '31-Dec-2008')
  1389. ->setCellValue('A3', '14-Feb-2008');
  1390. $worksheet->setCellValue('B2', '=WEEKDAY(A2)')
  1391. ->setCellValue('B3', '=WEEKDAY(A3,0)')
  1392. ->setCellValue('B4', '=WEEKDAY(A3,2)');
  1393. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1394. // $retVal = 12
  1395. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1396. // $retVal = 2
  1397. $retVal = $worksheet->getCell('B4')->getCalculatedValue();
  1398. // $retVal = 2
  1399. ```
  1400. ``` php
  1401. $retVal = call_user_func_array(
  1402. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'WEEKDAY'],
  1403. ['14-July-2008']
  1404. );
  1405. // $retVal = 7
  1406. ```
  1407. ##### Notes
  1408. Note that the PhpSpreadsheet function is
  1409. `\PhpOffice\PhpSpreadsheet\Calculation\Functions::WEEKDAY()` when the
  1410. method is called statically.
  1411. #### WEEKNUM
  1412. Not yet documented.
  1413. #### WORKDAY
  1414. Not yet documented.
  1415. #### YEAR
  1416. The YEAR function returns the year of a date.
  1417. ##### Syntax
  1418. YEAR(datetime)
  1419. ##### Parameters
  1420. **datetime** Date.
  1421. An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date
  1422. represented as a string.
  1423. ##### Return Value
  1424. **integer** An integer value that reflects the month of the year.
  1425. This is an integer year value.
  1426. ##### Examples
  1427. ``` php
  1428. $worksheet->setCellValue('A1', 'Date String')
  1429. ->setCellValue('A2', '17-Jul-1982')
  1430. ->setCellValue('A3', '16-Apr-2009');
  1431. $worksheet->setCellValue('B2', '=YEAR(A2)')
  1432. ->setCellValue('B3', '=YEAR(A3)');
  1433. $retVal = $worksheet->getCell('B2')->getCalculatedValue();
  1434. // $retVal = 1982
  1435. $retVal = $worksheet->getCell('B3')->getCalculatedValue();
  1436. // $retVal = 2009
  1437. ```
  1438. ``` php
  1439. $retVal = call_user_func_array(
  1440. ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'YEAR'],
  1441. ['14-July-2001']
  1442. );
  1443. // $retVal = 2001
  1444. ```
  1445. ##### Notes
  1446. There are no additional notes on this function
  1447. ### YEARFRAC
  1448. Not yet documented.