| 1 |
# Calculation Engine |
| 2 |
|
| 3 |
## Using the PhpSpreadsheet calculation engine |
| 4 |
|
| 5 |
### Performing formula calculations |
| 6 |
|
| 7 |
As PhpSpreadsheet represents an in-memory spreadsheet, it also offers |
| 8 |
formula calculation capabilities. A cell can be of a value type |
| 9 |
(containing a number or text), or a formula type (containing a formula |
| 10 |
which can be evaluated). For example, the formula `=SUM(A1:A10)` |
| 11 |
evaluates to the sum of values in A1, A2, ..., A10. |
| 12 |
|
| 13 |
To calculate a formula, you can call the cell containing the formula’s |
| 14 |
method `getCalculatedValue()`, for example: |
| 15 |
|
| 16 |
``` php |
| 17 |
$spreadsheet->getActiveSheet()->getCell('E11')->getCalculatedValue(); |
| 18 |
``` |
| 19 |
|
| 20 |
If you write the following line of code in the invoice demo included |
| 21 |
with PhpSpreadsheet, it evaluates to the value "64": |
| 22 |
|
| 23 |
 |
| 24 |
|
| 25 |
Another nice feature of PhpSpreadsheet's formula parser, is that it can |
| 26 |
automatically adjust a formula when inserting/removing rows/columns. |
| 27 |
Here's an example: |
| 28 |
|
| 29 |
 |
| 30 |
|
| 31 |
You see that the formula contained in cell E11 is "SUM(E4:E9)". Now, |
| 32 |
when I write the following line of code, two new product lines are |
| 33 |
added: |
| 34 |
|
| 35 |
``` php |
| 36 |
$spreadsheet->getActiveSheet()->insertNewRowBefore(7, 2); |
| 37 |
``` |
| 38 |
|
| 39 |
 |
| 40 |
|
| 41 |
Did you notice? The formula in the former cell E11 (now E13, as I |
| 42 |
inserted 2 new rows), changed to "SUM(E4:E11)". Also, the inserted cells |
| 43 |
duplicate style information of the previous cell, just like Excel's |
| 44 |
behaviour. Note that you can both insert rows and columns. |
| 45 |
|
| 46 |
## Calculation Cache |
| 47 |
|
| 48 |
Once the Calculation engine has evaluated the formula in a cell, the result |
| 49 |
will be cached, so if you call `getCalculatedValue()` a second time for the |
| 50 |
same cell, the result will be returned from the cache rather than evaluating |
| 51 |
the formula a second time. This helps boost performance, because evaluating |
| 52 |
a formula is an expensive operation in terms of performance and speed. |
| 53 |
|
| 54 |
However, there may be times when you don't want this, perhaps you've changed |
| 55 |
the underlying data and need to re-evaluate the same formula with that new |
| 56 |
data. |
| 57 |
|
| 58 |
``` |
| 59 |
Calculation::getInstance($spreadsheet)->disableCalculationCache(); |
| 60 |
``` |
| 61 |
|
| 62 |
Will disable calculation caching, and flush the current calculation cache. |
| 63 |
|
| 64 |
If you want only to flush the cache, then you can call |
| 65 |
|
| 66 |
``` |
| 67 |
Calculation::getInstance($spreadsheet)->clearCalculationCache(); |
| 68 |
``` |
| 69 |
|
| 70 |
## Known limitations |
| 71 |
|
| 72 |
There are some known limitations to the PhpSpreadsheet calculation |
| 73 |
engine. Most of them are due to the fact that an Excel formula is |
| 74 |
converted into PHP code before being executed. This means that Excel |
| 75 |
formula calculation is subject to PHP's language characteristics. |
| 76 |
|
| 77 |
### Function that are not Supported in Xls |
| 78 |
|
| 79 |
Not all functions are supported, for a comprehensive list, read the |
| 80 |
[](../references/function-list-by-name.mdfunction list by name](../references/function-list-by-name.md](../references/function-list-by-name.md). |
| 81 |
|
| 82 |
#### Operator precedence |
| 83 |
|
| 84 |
In Excel `+` wins over `&`, just like `*` wins over `+` in ordinary |
| 85 |
algebra. The former rule is not what one finds using the calculation |
| 86 |
engine shipped with PhpSpreadsheet. |
| 87 |
|
| 88 |
- [](https://support.office.com/en-us/article/Calculation-operators-and-precedence-in-Excel-48be406d-4975-4d31-b2b8-7af9e0e2878aReference for Excel](https://support.office.com/en-us/article/Calculation-operators-and-precedence-in-Excel-48be406d-4975-4d31-b2b8-7af9e0e2878a](https://support.office.com/en-us/article/Calculation-operators-and-precedence-in-Excel-48be406d-4975-4d31-b2b8-7af9e0e2878a) |
| 89 |
- [](https://php.net/manual/en/language.operators.phpReference for PHP](https://php.net/manual/en/language.operators.php](https://php.net/manual/en/language.operators.php) |
| 90 |
|
| 91 |
#### Formulas involving numbers and text |
| 92 |
|
| 93 |
Formulas involving numbers and text may produce unexpected results or |
| 94 |
even unreadable file contents. For example, the formula `=3+"Hello "` is |
| 95 |
expected to produce an error in Excel (\#VALUE!). Due to the fact that |
| 96 |
PHP converts `"Hello "` to a numeric value (zero), the result of this |
| 97 |
formula is evaluated as 3 instead of evaluating as an error. This also |
| 98 |
causes the Excel document being generated as containing unreadable |
| 99 |
content. |
| 100 |
|
| 101 |
- [](https://php.net/manual/en/language.types.string.php#language.types.string.conversionReference for this behaviour in PHP](https://php.net/manual/en/language.types.string.php#language.types.string.conversion](https://php.net/manual/en/language.types.string.php#language.types.string.conversion) |
| 102 |
|
| 103 |
#### Formulas don’t seem to be calculated in Excel2003 using compatibility pack? |
| 104 |
|
| 105 |
This is normal behaviour of the compatibility pack, Xlsx displays this |
| 106 |
correctly. Use `\PhpOffice\PhpSpreadsheet\Writer\Xls` if you really need |
| 107 |
calculated values, or force recalculation in Excel2003. |
| 108 |
|
| 109 |
## Handling Date and Time Values |
| 110 |
|
| 111 |
### Excel functions that return a Date and Time value |
| 112 |
|
| 113 |
Any of the Date and Time functions that return a date value in Excel can |
| 114 |
return either an Excel timestamp or a PHP timestamp or `DateTime` object. |
| 115 |
|
| 116 |
It is possible for scripts to change the data type used for returning |
| 117 |
date values by calling the |
| 118 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType()` |
| 119 |
method: |
| 120 |
|
| 121 |
``` php |
| 122 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($returnDateType); |
| 123 |
``` |
| 124 |
|
| 125 |
where the following constants can be used for `$returnDateType`: |
| 126 |
|
| 127 |
- `\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC` |
| 128 |
- `\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_OBJECT` |
| 129 |
- `\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL` |
| 130 |
|
| 131 |
The method will return a Boolean True on success, False on failure (e.g. |
| 132 |
if an invalid value is passed in for the return date type). |
| 133 |
|
| 134 |
The `\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()` |
| 135 |
method can be used to determine the current value of this setting: |
| 136 |
|
| 137 |
``` php |
| 138 |
$returnDateType = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType(); |
| 139 |
``` |
| 140 |
|
| 141 |
The default is `RETURNDATE_PHP_NUMERIC`. |
| 142 |
|
| 143 |
#### PHP Timestamps |
| 144 |
|
| 145 |
If `RETURNDATE_PHP_NUMERIC` is set for the Return Date Type, then any |
| 146 |
date value returned to the calling script by any access to the Date and |
| 147 |
Time functions in Excel will be an integer value that represents the |
| 148 |
number of seconds from the PHP/Unix base date. The PHP/Unix base date |
| 149 |
(0) is 00:00 UST on 1st January 1970. This value can be positive or |
| 150 |
negative: so a value of -3600 would be 23:00 hrs on 31st December 1969; |
| 151 |
while a value of +3600 would be 01:00 hrs on 1st January 1970. This |
| 152 |
gives PHP a date range of between 14th December 1901 and 19th January |
| 153 |
2038. |
| 154 |
|
| 155 |
#### PHP `DateTime` Objects |
| 156 |
|
| 157 |
If the Return Date Type is set for `RETURNDATE_PHP_OBJECT`, then any |
| 158 |
date value returned to the calling script by any access to the Date and |
| 159 |
Time functions in Excel will be a PHP `DateTime` object. |
| 160 |
|
| 161 |
#### Excel Timestamps |
| 162 |
|
| 163 |
If `RETURNDATE_EXCEL` is set for the Return Date Type, then the returned |
| 164 |
date value by any access to the Date and Time functions in Excel will be |
| 165 |
a floating point value that represents a number of days from the Excel |
| 166 |
base date. The Excel base date is determined by which calendar Excel |
| 167 |
uses: the Windows 1900 or the Mac 1904 calendar. 1st January 1900 is the |
| 168 |
base date for the Windows 1900 calendar while 1st January 1904 is the |
| 169 |
base date for the Mac 1904 calendar. |
| 170 |
|
| 171 |
It is possible for scripts to change the calendar used for calculating |
| 172 |
Excel date values by calling the |
| 173 |
`\PhpOffice\PhpSpreadsheet\Shared\Date::setExcelCalendar()` method: |
| 174 |
|
| 175 |
``` php |
| 176 |
\PhpOffice\PhpSpreadsheet\Shared\Date::setExcelCalendar($baseDate); |
| 177 |
``` |
| 178 |
|
| 179 |
where the following constants can be used for `$baseDate`: |
| 180 |
|
| 181 |
- `\PhpOffice\PhpSpreadsheet\Shared\Date::CALENDAR_WINDOWS_1900` |
| 182 |
- `\PhpOffice\PhpSpreadsheet\Shared\Date::CALENDAR_MAC_1904` |
| 183 |
|
| 184 |
The method will return a Boolean True on success, False on failure (e.g. |
| 185 |
if an invalid value is passed in). |
| 186 |
|
| 187 |
The `\PhpOffice\PhpSpreadsheet\Shared\Date::getExcelCalendar()` method can |
| 188 |
be used to determine the current value of this setting: |
| 189 |
|
| 190 |
``` php |
| 191 |
$baseDate = \PhpOffice\PhpSpreadsheet\Shared\Date::getExcelCalendar(); |
| 192 |
``` |
| 193 |
|
| 194 |
The default is `CALENDAR_WINDOWS_1900`. |
| 195 |
|
| 196 |
#### Functions that return a Date/Time Value |
| 197 |
|
| 198 |
- DATE |
| 199 |
- DATEVALUE |
| 200 |
- EDATE |
| 201 |
- EOMONTH |
| 202 |
- NOW |
| 203 |
- TIME |
| 204 |
- TIMEVALUE |
| 205 |
- TODAY |
| 206 |
|
| 207 |
### Excel functions that accept Date and Time values as parameters |
| 208 |
|
| 209 |
Date values passed in as parameters to a function can be an Excel |
| 210 |
timestamp or a PHP timestamp; or `DateTime` object; or a string containing a |
| 211 |
date value (e.g. '1-Jan-2009'). PhpSpreadsheet will attempt to identify |
| 212 |
their type based on the PHP datatype: |
| 213 |
|
| 214 |
An integer numeric value will be treated as a PHP/Unix timestamp. A real |
| 215 |
(floating point) numeric value will be treated as an Excel |
| 216 |
date/timestamp. Any PHP `DateTime` object will be treated as a `DateTime` |
| 217 |
object. Any string value (even one containing straight numeric data) |
| 218 |
will be converted to a `DateTime` object for validation as a date value |
| 219 |
based on the server locale settings, so passing through an ambiguous |
| 220 |
value of '07/08/2008' will be treated as 7th August 2008 if your server |
| 221 |
settings are UK, but as 8th July 2008 if your server settings are US. |
| 222 |
However, if you pass through a value such as '31/12/2008' that would be |
| 223 |
considered an error by a US-based server, but which is not ambiguous, |
| 224 |
then PhpSpreadsheet will attempt to correct this to 31st December 2008. |
| 225 |
If the content of the string doesn’t match any of the formats recognised |
| 226 |
by the php `DateTime` object implementation of `strtotime()` (which can |
| 227 |
handle a wider range of formats than the normal `strtotime()` function), |
| 228 |
then the function will return a `#VALUE` error. However, Excel |
| 229 |
recommends that you should always use date/timestamps for your date |
| 230 |
functions, and the recommendation for PhpSpreadsheet is the same: avoid |
| 231 |
strings because the result is not predictable. |
| 232 |
|
| 233 |
The same principle applies when data is being written to Excel. Cells |
| 234 |
containing date actual values (rather than Excel functions that return a |
| 235 |
date value) are always written as Excel dates, converting where |
| 236 |
necessary. If a cell formatted as a date contains an integer or |
| 237 |
`DateTime` object value, then it is converted to an Excel value for |
| 238 |
writing: if a cell formatted as a date contains a real value, then no |
| 239 |
conversion is required. Note that string values are written as strings |
| 240 |
rather than converted to Excel date timestamp values. |
| 241 |
|
| 242 |
#### Functions that expect a Date/Time Value |
| 243 |
|
| 244 |
- DATEDIF |
| 245 |
- DAY |
| 246 |
- DAYS360 |
| 247 |
- EDATE |
| 248 |
- EOMONTH |
| 249 |
- HOUR |
| 250 |
- MINUTE |
| 251 |
- MONTH |
| 252 |
- NETWORKDAYS |
| 253 |
- SECOND |
| 254 |
- WEEKDAY |
| 255 |
- WEEKNUM |
| 256 |
- WORKDAY |
| 257 |
- YEAR |
| 258 |
- YEARFRAC |
| 259 |
|
| 260 |
### Helper Methods |
| 261 |
|
| 262 |
In addition to the `setExcelCalendar()` and `getExcelCalendar()` methods, a |
| 263 |
number of other methods are available in the |
| 264 |
`\PhpOffice\PhpSpreadsheet\Shared\Date` class that can help when working |
| 265 |
with dates: |
| 266 |
|
| 267 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($excelDate) |
| 268 |
|
| 269 |
Converts a date/time from an Excel date timestamp to return a PHP |
| 270 |
serialized date/timestamp. |
| 271 |
|
| 272 |
Note that this method does not trap for Excel dates that fall outside of |
| 273 |
the valid range for a PHP date timestamp. |
| 274 |
|
| 275 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($excelDate) |
| 276 |
|
| 277 |
Converts a date from an Excel date/timestamp to return a PHP `DateTime` |
| 278 |
object. |
| 279 |
|
| 280 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($PHPDate) |
| 281 |
|
| 282 |
Converts a PHP serialized date/timestamp or a PHP `DateTime` object to |
| 283 |
return an Excel date timestamp. |
| 284 |
|
| 285 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($year, $month, $day, $hours=0, $minutes=0, $seconds=0) |
| 286 |
|
| 287 |
Takes year, month and day values (and optional hour, minute and second |
| 288 |
values) and returns an Excel date timestamp value. |
| 289 |
|
| 290 |
### Timezone support for Excel date timestamp conversions |
| 291 |
|
| 292 |
The default timezone for the date functions in PhpSpreadsheet is UST (Universal Standard Time). |
| 293 |
If a different timezone needs to be used, these methods are available: |
| 294 |
|
| 295 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::getDefaultTimezone() |
| 296 |
|
| 297 |
Returns the current timezone value PhpSpeadsheet is using to handle dates and times. |
| 298 |
|
| 299 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::setDefaultTimezone($timeZone) |
| 300 |
|
| 301 |
Sets the timezone for Excel date timestamp conversions to $timeZone, |
| 302 |
which must be a valid PHP DateTimeZone value. |
| 303 |
The return value is a Boolean, where true is success, |
| 304 |
and false is failure (e.g. an invalid DateTimeZone value was passed.) |
| 305 |
|
| 306 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($excelDate, $timeZone) |
| 307 |
#### \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimeStamp($excelDate, $timeZone) |
| 308 |
|
| 309 |
These functions support a timezone as an optional second parameter. |
| 310 |
This applies a specific timezone to that function call without affecting the default PhpSpreadsheet Timezone. |
| 311 |
|
| 312 |
## Function Reference |
| 313 |
|
| 314 |
### Database Functions |
| 315 |
|
| 316 |
#### DAVERAGE |
| 317 |
|
| 318 |
The DAVERAGE function returns the average value of the cells in a column |
| 319 |
of a list or database that match conditions you specify. |
| 320 |
|
| 321 |
##### Syntax |
| 322 |
|
| 323 |
DAVERAGE (database, field, criteria) |
| 324 |
|
| 325 |
##### Parameters |
| 326 |
|
| 327 |
**database** The range of cells that makes up the list or database. |
| 328 |
|
| 329 |
A database is a list of related data in which rows of related |
| 330 |
information are records, and columns of data are fields. The first row |
| 331 |
of the list contains labels for each column. |
| 332 |
|
| 333 |
**field** Indicates which column of the database is used in the |
| 334 |
function. |
| 335 |
|
| 336 |
Enter the column label as a string (enclosed between double quotation |
| 337 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 338 |
marks) that represents the position of the column within the list: 1 for |
| 339 |
the first column, 2 for the second column, and so on. |
| 340 |
|
| 341 |
**criteria** The range of cells that contains the conditions you |
| 342 |
specify. |
| 343 |
|
| 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 |
|
| 348 |
##### Return Value |
| 349 |
|
| 350 |
**float** The average value of the matching cells. |
| 351 |
|
| 352 |
This is the statistical mean. |
| 353 |
|
| 354 |
##### Examples |
| 355 |
|
| 356 |
``` php |
| 357 |
$database = [ |
| 358 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 359 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 360 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 361 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 362 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 363 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 364 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 365 |
]; |
| 366 |
|
| 367 |
$criteria = [ |
| 368 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 369 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 370 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 371 |
]; |
| 372 |
|
| 373 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 374 |
->fromArray( $database, NULL, 'A4' ); |
| 375 |
|
| 376 |
$worksheet->setCellValue('A12', '=DAVERAGE(A4:E10,"Yield",A1:B2)'); |
| 377 |
|
| 378 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 379 |
// $retVal = 12 |
| 380 |
``` |
| 381 |
|
| 382 |
##### Notes |
| 383 |
|
| 384 |
There are no additional notes on this function |
| 385 |
|
| 386 |
#### DCOUNT |
| 387 |
|
| 388 |
The DCOUNT function returns the count of cells that contain a number in |
| 389 |
a column of a list or database matching conditions that you specify. |
| 390 |
|
| 391 |
##### Syntax |
| 392 |
|
| 393 |
DCOUNT(database, [field], criteria) |
| 394 |
|
| 395 |
##### Parameters |
| 396 |
|
| 397 |
**database** The range of cells that makes up the list or database. |
| 398 |
|
| 399 |
A database is a list of related data in which rows of related |
| 400 |
information are records, and columns of data are fields. The first row |
| 401 |
of the list contains labels for each column. |
| 402 |
|
| 403 |
**field** Indicates which column of the database is used in the |
| 404 |
function. |
| 405 |
|
| 406 |
Enter the column label as a string (enclosed between double quotation |
| 407 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 408 |
marks) that represents the position of the column within the list: 1 for |
| 409 |
the first column, 2 for the second column, and so on. |
| 410 |
|
| 411 |
**criteria** The range of cells that contains the conditions you |
| 412 |
specify. |
| 413 |
|
| 414 |
You can use any range for the criteria argument, as long as it includes |
| 415 |
at least one column label and at least one cell below the column label |
| 416 |
in which you specify a condition for the column. |
| 417 |
|
| 418 |
##### Return Value |
| 419 |
|
| 420 |
**float** The count of the matching cells. |
| 421 |
|
| 422 |
##### Examples |
| 423 |
|
| 424 |
``` php |
| 425 |
$database = [ |
| 426 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 427 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 428 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 429 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 430 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 431 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 432 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 433 |
]; |
| 434 |
|
| 435 |
$criteria = [ |
| 436 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 437 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 438 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 439 |
]; |
| 440 |
|
| 441 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 442 |
->fromArray( $database, NULL, 'A4' ); |
| 443 |
|
| 444 |
$worksheet->setCellValue('A12', '=DCOUNT(A4:E10,"Height",A1:B3)'); |
| 445 |
|
| 446 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 447 |
|
| 448 |
// $retVal = 3 |
| 449 |
``` |
| 450 |
|
| 451 |
##### Notes |
| 452 |
|
| 453 |
In MS Excel, The field argument is optional. If field is omitted, DCOUNT |
| 454 |
counts all records in the database that match the criteria. This logic |
| 455 |
has not yet been implemented in PhpSpreadsheet. |
| 456 |
|
| 457 |
#### DCOUNTA |
| 458 |
|
| 459 |
The DCOUNT function returns the count of cells that aren’t blank in a |
| 460 |
column of a list or database and that match conditions that you specify. |
| 461 |
|
| 462 |
##### Syntax |
| 463 |
|
| 464 |
DCOUNTA(database, [field], criteria) |
| 465 |
|
| 466 |
##### Parameters |
| 467 |
|
| 468 |
**database** The range of cells that makes up the list or database. |
| 469 |
|
| 470 |
A database is a list of related data in which rows of related |
| 471 |
information are records, and columns of data are fields. The first row |
| 472 |
of the list contains labels for each column. |
| 473 |
|
| 474 |
**field** Indicates which column of the database is used in the |
| 475 |
function. |
| 476 |
|
| 477 |
Enter the column label as a string (enclosed between double quotation |
| 478 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 479 |
marks) that represents the position of the column within the list: 1 for |
| 480 |
the first column, 2 for the second column, and so on. |
| 481 |
|
| 482 |
**criteria** The range of cells that contains the conditions you |
| 483 |
specify. |
| 484 |
|
| 485 |
You can use any range for the criteria argument, as long as it includes |
| 486 |
at least one column label and at least one cell below the column label |
| 487 |
in which you specify a condition for the column. |
| 488 |
|
| 489 |
##### Return Value |
| 490 |
|
| 491 |
**float** The count of the matching cells. |
| 492 |
|
| 493 |
##### Examples |
| 494 |
|
| 495 |
``` php |
| 496 |
$database = [ |
| 497 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 498 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 499 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 500 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 501 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 502 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 503 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 504 |
]; |
| 505 |
|
| 506 |
$criteria = [ |
| 507 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 508 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 509 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 510 |
]; |
| 511 |
|
| 512 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 513 |
->fromArray( $database, NULL, 'A4' ); |
| 514 |
|
| 515 |
$worksheet->setCellValue('A12', '=DCOUNTA(A4:E10,"Yield",A1:A3)'); |
| 516 |
|
| 517 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 518 |
|
| 519 |
// $retVal = 5 |
| 520 |
``` |
| 521 |
|
| 522 |
##### Notes |
| 523 |
|
| 524 |
In MS Excel, The field argument is optional. If field is omitted, |
| 525 |
DCOUNTA counts all records in the database that match the criteria. This |
| 526 |
logic has not yet been implemented in PhpSpreadsheet. |
| 527 |
|
| 528 |
#### DGET |
| 529 |
|
| 530 |
The DGET function extracts a single value from a column of a list or |
| 531 |
database that matches conditions that you specify. |
| 532 |
|
| 533 |
##### Syntax |
| 534 |
|
| 535 |
DGET(database, field, criteria) |
| 536 |
|
| 537 |
##### Parameters |
| 538 |
|
| 539 |
**database** The range of cells that makes up the list or database. |
| 540 |
|
| 541 |
A database is a list of related data in which rows of related |
| 542 |
information are records, and columns of data are fields. The first row |
| 543 |
of the list contains labels for each column. |
| 544 |
|
| 545 |
**field** Indicates which column of the database is used in the |
| 546 |
function. |
| 547 |
|
| 548 |
Enter the column label as a string (enclosed between double quotation |
| 549 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 550 |
marks) that represents the position of the column within the list: 1 for |
| 551 |
the first column, 2 for the second column, and so on. |
| 552 |
|
| 553 |
**criteria** The range of cells that contains the conditions you |
| 554 |
specify. |
| 555 |
|
| 556 |
You can use any range for the criteria argument, as long as it includes |
| 557 |
at least one column label and at least one cell below the column label |
| 558 |
in which you specify a condition for the column. |
| 559 |
|
| 560 |
##### Return Value |
| 561 |
|
| 562 |
**mixed** The value from the selected column of the matching row. |
| 563 |
|
| 564 |
#### Examples |
| 565 |
|
| 566 |
``` php |
| 567 |
$database = [ |
| 568 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 569 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 570 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 571 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 572 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 573 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 574 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 575 |
]; |
| 576 |
|
| 577 |
$criteria = [ |
| 578 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 579 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 580 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 581 |
]; |
| 582 |
|
| 583 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 584 |
->fromArray( $database, NULL, 'A4' ); |
| 585 |
|
| 586 |
$worksheet->setCellValue('A12', '=GET(A4:E10,"Age",A1:F2)'); |
| 587 |
|
| 588 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 589 |
// $retVal = 14 |
| 590 |
``` |
| 591 |
|
| 592 |
##### Notes |
| 593 |
|
| 594 |
There are no additional notes on this function |
| 595 |
|
| 596 |
#### DMAX |
| 597 |
|
| 598 |
The DMAX function returns the largest number in a column of a list or |
| 599 |
database that matches conditions you specify. |
| 600 |
|
| 601 |
##### Syntax |
| 602 |
|
| 603 |
DMAX(database, field, criteria) |
| 604 |
|
| 605 |
##### Parameters |
| 606 |
|
| 607 |
**database** The range of cells that makes up the list or database. |
| 608 |
|
| 609 |
A database is a list of related data in which rows of related |
| 610 |
information are records, and columns of data are fields. The first row |
| 611 |
of the list contains labels for each column. |
| 612 |
|
| 613 |
**field** Indicates which column of the database is used in the |
| 614 |
function. |
| 615 |
|
| 616 |
Enter the column label as a string (enclosed between double quotation |
| 617 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 618 |
marks) that represents the position of the column within the list: 1 for |
| 619 |
the first column, 2 for the second column, and so on. |
| 620 |
|
| 621 |
**criteria** The range of cells that contains the conditions you |
| 622 |
specify. |
| 623 |
|
| 624 |
You can use any range for the criteria argument, as long as it includes |
| 625 |
at least one column label and at least one cell below the column label |
| 626 |
in which you specify a condition for the column. |
| 627 |
|
| 628 |
##### Return Value |
| 629 |
|
| 630 |
**float** The maximum value of the matching cells. |
| 631 |
|
| 632 |
##### Examples |
| 633 |
|
| 634 |
``` php |
| 635 |
$database = [ |
| 636 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 637 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 638 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 639 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 640 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 641 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 642 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 643 |
]; |
| 644 |
|
| 645 |
$criteria = [ |
| 646 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 647 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 648 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 649 |
]; |
| 650 |
|
| 651 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 652 |
->fromArray( $database, NULL, 'A4' ); |
| 653 |
|
| 654 |
$worksheet->setCellValue('A12', '=DMAX(A4:E10,"Profit",A1:B2)'); |
| 655 |
|
| 656 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 657 |
// $retVal = 105 |
| 658 |
``` |
| 659 |
|
| 660 |
##### Notes |
| 661 |
|
| 662 |
There are no additional notes on this function |
| 663 |
|
| 664 |
#### DMIN |
| 665 |
|
| 666 |
The DMIN function returns the smallest number in a column of a list or |
| 667 |
database that matches conditions you specify. |
| 668 |
|
| 669 |
##### Syntax |
| 670 |
|
| 671 |
DMIN(database, field, criteria) |
| 672 |
|
| 673 |
##### Parameters |
| 674 |
|
| 675 |
**database** The range of cells that makes up the list or database. |
| 676 |
|
| 677 |
A database is a list of related data in which rows of related |
| 678 |
information are records, and columns of data are fields. The first row |
| 679 |
of the list contains labels for each column. |
| 680 |
|
| 681 |
**field** Indicates which column of the database is used in the |
| 682 |
function. |
| 683 |
|
| 684 |
Enter the column label as a string (enclosed between double quotation |
| 685 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 686 |
marks) that represents the position of the column within the list: 1 for |
| 687 |
the first column, 2 for the second column, and so on. |
| 688 |
|
| 689 |
**criteria** The range of cells that contains the conditions you |
| 690 |
specify. |
| 691 |
|
| 692 |
You can use any range for the criteria argument, as long as it includes |
| 693 |
at least one column label and at least one cell below the column label |
| 694 |
in which you specify a condition for the column. |
| 695 |
|
| 696 |
##### Return Value |
| 697 |
|
| 698 |
**float** The minimum value of the matching cells. |
| 699 |
|
| 700 |
##### Examples |
| 701 |
|
| 702 |
``` php |
| 703 |
$database = [ |
| 704 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 705 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 706 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 707 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 708 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 709 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 710 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 711 |
]; |
| 712 |
|
| 713 |
$criteria = [ |
| 714 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 715 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 716 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 717 |
]; |
| 718 |
|
| 719 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 720 |
->fromArray( $database, NULL, 'A4' ); |
| 721 |
|
| 722 |
$worksheet->setCellValue('A12', '=DMIN(A4:E10,"Yield",A1:A3)'); |
| 723 |
|
| 724 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 725 |
// $retVal = 6 |
| 726 |
``` |
| 727 |
|
| 728 |
##### Notes |
| 729 |
|
| 730 |
There are no additional notes on this function |
| 731 |
|
| 732 |
#### DPRODUCT |
| 733 |
|
| 734 |
The DPRODUCT function multiplies the values in a column of a list or |
| 735 |
database that match conditions that you specify. |
| 736 |
|
| 737 |
##### Syntax |
| 738 |
|
| 739 |
DPRODUCT(database, field, criteria) |
| 740 |
|
| 741 |
##### Parameters |
| 742 |
|
| 743 |
**database** The range of cells that makes up the list or database. |
| 744 |
|
| 745 |
A database is a list of related data in which rows of related |
| 746 |
information are records, and columns of data are fields. The first row |
| 747 |
of the list contains labels for each column. |
| 748 |
|
| 749 |
**field** Indicates which column of the database is used in the |
| 750 |
function. |
| 751 |
|
| 752 |
Enter the column label as a string (enclosed between double quotation |
| 753 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 754 |
marks) that represents the position of the column within the list: 1 for |
| 755 |
the first column, 2 for the second column, and so on. |
| 756 |
|
| 757 |
**criteria** The range of cells that contains the conditions you |
| 758 |
specify. |
| 759 |
|
| 760 |
You can use any range for the criteria argument, as long as it includes |
| 761 |
at least one column label and at least one cell below the column label |
| 762 |
in which you specify a condition for the column. |
| 763 |
|
| 764 |
##### Return Value |
| 765 |
|
| 766 |
**float** The product of the matching cells. |
| 767 |
|
| 768 |
##### Examples |
| 769 |
|
| 770 |
``` php |
| 771 |
$database = [ |
| 772 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 773 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 774 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 775 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 776 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 777 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 778 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 779 |
]; |
| 780 |
|
| 781 |
$criteria = [ |
| 782 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 783 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 784 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 785 |
]; |
| 786 |
|
| 787 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 788 |
->fromArray( $database, NULL, 'A4' ); |
| 789 |
|
| 790 |
$worksheet->setCellValue('A12', '=DPRODUCT(A4:E10,"Yield",A1:B2)'); |
| 791 |
|
| 792 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 793 |
// $retVal = 140 |
| 794 |
``` |
| 795 |
|
| 796 |
##### Notes |
| 797 |
|
| 798 |
There are no additional notes on this function |
| 799 |
|
| 800 |
#### DSTDEV |
| 801 |
|
| 802 |
The DSTDEV function estimates the standard deviation of a population |
| 803 |
based on a sample by using the numbers in a column of a list or database |
| 804 |
that match conditions that you specify. |
| 805 |
|
| 806 |
##### Syntax |
| 807 |
|
| 808 |
DSTDEV(database, field, criteria) |
| 809 |
|
| 810 |
##### Parameters |
| 811 |
|
| 812 |
**database** The range of cells that makes up the list or database. |
| 813 |
|
| 814 |
A database is a list of related data in which rows of related |
| 815 |
information are records, and columns of data are fields. The first row |
| 816 |
of the list contains labels for each column. |
| 817 |
|
| 818 |
**field** Indicates which column of the database is used in the |
| 819 |
function. |
| 820 |
|
| 821 |
Enter the column label as a string (enclosed between double quotation |
| 822 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 823 |
marks) that represents the position of the column within the list: 1 for |
| 824 |
the first column, 2 for the second column, and so on. |
| 825 |
|
| 826 |
**criteria** The range of cells that contains the conditions you |
| 827 |
specify. |
| 828 |
|
| 829 |
You can use any range for the criteria argument, as long as it includes |
| 830 |
at least one column label and at least one cell below the column label |
| 831 |
in which you specify a condition for the column. |
| 832 |
|
| 833 |
##### Return Value |
| 834 |
|
| 835 |
**float** The estimated standard deviation of the matching cells. |
| 836 |
|
| 837 |
##### Examples |
| 838 |
|
| 839 |
``` php |
| 840 |
$database = [ |
| 841 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 842 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 843 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 844 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 845 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 846 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 847 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 848 |
]; |
| 849 |
|
| 850 |
$criteria = [ |
| 851 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 852 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 853 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 854 |
]; |
| 855 |
|
| 856 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 857 |
->fromArray( $database, NULL, 'A4' ); |
| 858 |
|
| 859 |
$worksheet->setCellValue('A12', '=DSTDEV(A4:E10,"Yield",A1:A3)'); |
| 860 |
|
| 861 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 862 |
// $retVal = 2.97 |
| 863 |
``` |
| 864 |
|
| 865 |
##### Notes |
| 866 |
|
| 867 |
There are no additional notes on this function |
| 868 |
|
| 869 |
#### DSTDEVP |
| 870 |
|
| 871 |
The DSTDEVP function calculates the standard deviation of a population |
| 872 |
based on the entire population by using the numbers in a column of a |
| 873 |
list or database that match conditions that you specify. |
| 874 |
|
| 875 |
##### Syntax |
| 876 |
|
| 877 |
DSTDEVP(database, field, criteria) |
| 878 |
|
| 879 |
##### Parameters |
| 880 |
|
| 881 |
**database** The range of cells that makes up the list or database. |
| 882 |
|
| 883 |
A database is a list of related data in which rows of related |
| 884 |
information are records, and columns of data are fields. The first row |
| 885 |
of the list contains labels for each column. |
| 886 |
|
| 887 |
**field** Indicates which column of the database is used in the |
| 888 |
function. |
| 889 |
|
| 890 |
Enter the column label as a string (enclosed between double quotation |
| 891 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 892 |
marks) that represents the position of the column within the list: 1 for |
| 893 |
the first column, 2 for the second column, and so on. |
| 894 |
|
| 895 |
**criteria** The range of cells that contains the conditions you |
| 896 |
specify. |
| 897 |
|
| 898 |
You can use any range for the criteria argument, as long as it includes |
| 899 |
at least one column label and at least one cell below the column label |
| 900 |
in which you specify a condition for the column. |
| 901 |
|
| 902 |
##### Return Value |
| 903 |
|
| 904 |
**float** The estimated standard deviation of the matching cells. |
| 905 |
|
| 906 |
##### Examples |
| 907 |
|
| 908 |
``` php |
| 909 |
$database = [ |
| 910 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 911 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 912 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 913 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 914 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 915 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 916 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 917 |
]; |
| 918 |
|
| 919 |
$criteria = [ |
| 920 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 921 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 922 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 923 |
]; |
| 924 |
|
| 925 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 926 |
->fromArray( $database, NULL, 'A4' ); |
| 927 |
|
| 928 |
$worksheet->setCellValue('A12', '=DSTDEVP(A4:E10,"Yield",A1:A3)'); |
| 929 |
|
| 930 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 931 |
// $retVal = 2.65 |
| 932 |
``` |
| 933 |
|
| 934 |
##### Notes |
| 935 |
|
| 936 |
There are no additional notes on this function |
| 937 |
|
| 938 |
#### DSUM |
| 939 |
|
| 940 |
The DSUM function adds the numbers in a column of a list or database |
| 941 |
that matches conditions you specify. |
| 942 |
|
| 943 |
##### Syntax |
| 944 |
|
| 945 |
DSUM(database, field, criteria) |
| 946 |
|
| 947 |
##### Parameters |
| 948 |
|
| 949 |
**database** The range of cells that makes up the list or database. |
| 950 |
|
| 951 |
A database is a list of related data in which rows of related |
| 952 |
information are records, and columns of data are fields. The first row |
| 953 |
of the list contains labels for each column. |
| 954 |
|
| 955 |
**field** Indicates which column of the database is used in the |
| 956 |
function. |
| 957 |
|
| 958 |
Enter the column label as a string (enclosed between double quotation |
| 959 |
marks), such as "Age" or "Yield," or as a number (without quotation |
| 960 |
marks) that represents the position of the column within the list: 1 for |
| 961 |
the first column, 2 for the second column, and so on. |
| 962 |
|
| 963 |
**criteria** The range of cells that contains the conditions you |
| 964 |
specify. |
| 965 |
|
| 966 |
You can use any range for the criteria argument, as long as it includes |
| 967 |
at least one column label and at least one cell below the column label |
| 968 |
in which you specify a condition for the column. |
| 969 |
|
| 970 |
##### Return Value |
| 971 |
|
| 972 |
**float** The total value of the matching cells. |
| 973 |
|
| 974 |
##### Examples |
| 975 |
|
| 976 |
``` php |
| 977 |
$database = [ |
| 978 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit' ], |
| 979 |
[ 'Apple', 18, 20, 14, 105.00 ], |
| 980 |
[ 'Pear', 12, 12, 10, 96.00 ], |
| 981 |
[ 'Cherry', 13, 14, 9, 105.00 ], |
| 982 |
[ 'Apple', 14, 15, 10, 75.00 ], |
| 983 |
[ 'Pear', 9, 8, 8, 76.80 ], |
| 984 |
[ 'Apple', 8, 9, 6, 45.00 ], |
| 985 |
]; |
| 986 |
|
| 987 |
$criteria = [ |
| 988 |
[ 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ], |
| 989 |
[ '="=Apple"', '>10', NULL, NULL, NULL, '<16' ], |
| 990 |
[ '="=Pear"', NULL, NULL, NULL, NULL, NULL ], |
| 991 |
]; |
| 992 |
|
| 993 |
$worksheet->fromArray( $criteria, NULL, 'A1' ) |
| 994 |
->fromArray( $database, NULL, 'A4' ); |
| 995 |
|
| 996 |
$worksheet->setCellValue('A12', '=DMIN(A4:E10,"Profit",A1:A2)'); |
| 997 |
|
| 998 |
$retVal = $worksheet->getCell('A12')->getCalculatedValue(); |
| 999 |
// $retVal = 225 |
| 1000 |
``` |
| 1001 |
|
| 1002 |
##### Notes |
| 1003 |
|
| 1004 |
There are no additional notes on this function |
| 1005 |
|
| 1006 |
#### DVAR |
| 1007 |
|
| 1008 |
Not yet documented. |
| 1009 |
|
| 1010 |
#### DVARP |
| 1011 |
|
| 1012 |
Not yet documented. |
| 1013 |
|
| 1014 |
### Date and Time Functions |
| 1015 |
|
| 1016 |
Excel provides a number of functions for the manipulation of dates and |
| 1017 |
times, and calculations based on date/time values. it is worth spending |
| 1018 |
some time reading the section titled "Date and Time Values" on passing |
| 1019 |
date parameters and returning date values to understand how |
| 1020 |
PhpSpreadsheet reconciles the differences between dates and times in |
| 1021 |
Excel and in PHP. |
| 1022 |
|
| 1023 |
#### DATE |
| 1024 |
|
| 1025 |
The DATE function returns an Excel timestamp or a PHP timestamp or `DateTime` |
| 1026 |
object representing the date that is referenced by the parameters. |
| 1027 |
|
| 1028 |
##### Syntax |
| 1029 |
|
| 1030 |
DATE(year, month, day) |
| 1031 |
|
| 1032 |
##### Parameters |
| 1033 |
|
| 1034 |
**year** The year number. |
| 1035 |
|
| 1036 |
If this value is between 0 (zero) and 1899 inclusive (for the Windows |
| 1037 |
1900 calendar), or between 4 and 1903 inclusive (for the Mac 1904), then |
| 1038 |
PhpSpreadsheet adds it to the Calendar base year, so a value of 108 will |
| 1039 |
interpret the year as 2008 when using the Windows 1900 calendar, or 2012 |
| 1040 |
when using the Mac 1904 calendar. |
| 1041 |
|
| 1042 |
**month** The month number. |
| 1043 |
|
| 1044 |
If this value is greater than 12, the DATE function adds that number of |
| 1045 |
months to the first month in the year specified. For example, |
| 1046 |
DATE(2008,14,2) returns a value representing February 2, 2009. |
| 1047 |
|
| 1048 |
If the value of **month** is less than 1, then that value will be |
| 1049 |
adjusted by -1, and that will then be subtracted from the first month of |
| 1050 |
the year specified. For example, DATE(2008,0,2) returns a value |
| 1051 |
representing December 2, 2007; while DATE(2008,-1,2) returns a value |
| 1052 |
representing November 2, 2007. |
| 1053 |
|
| 1054 |
**day** The day number. |
| 1055 |
|
| 1056 |
If this value is greater than the number of days in the month (and year) |
| 1057 |
specified, the DATE function adds that number of days to the first day |
| 1058 |
in the month. For example, DATE(2008,1,35) returns a value representing |
| 1059 |
February 4, 2008. |
| 1060 |
|
| 1061 |
If the value of **day** is less than 1, then that value will be adjusted |
| 1062 |
by -1, and that will then be subtracted from the first month of the year |
| 1063 |
specified. For example, DATE(2008,3,0) returns a value representing |
| 1064 |
February 29, 2008; while DATE(2008,3,-2) returns a value representing |
| 1065 |
February 27, 2008. |
| 1066 |
|
| 1067 |
##### Return Value |
| 1068 |
|
| 1069 |
**mixed** A date/time stamp that corresponds to the given date. |
| 1070 |
|
| 1071 |
This could be a PHP timestamp value (integer), a PHP `DateTime` object, |
| 1072 |
or an Excel timestamp value (real), depending on the value of |
| 1073 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`. |
| 1074 |
|
| 1075 |
##### Examples |
| 1076 |
|
| 1077 |
``` php |
| 1078 |
$worksheet->setCellValue('A1', 'Year') |
| 1079 |
->setCellValue('A2', 'Month') |
| 1080 |
->setCellValue('A3', 'Day'); |
| 1081 |
|
| 1082 |
$worksheet->setCellValue('B1', 2008) |
| 1083 |
->setCellValue('B2', 12) |
| 1084 |
->setCellValue('B3', 31); |
| 1085 |
|
| 1086 |
$worksheet->setCellValue('D1', '=DATE(B1,B2,B3)'); |
| 1087 |
|
| 1088 |
$retVal = $worksheet->getCell('D1')->getCalculatedValue(); |
| 1089 |
// $retVal = 1230681600 |
| 1090 |
``` |
| 1091 |
|
| 1092 |
``` php |
| 1093 |
// We're going to be calling the same cell calculation multiple times, |
| 1094 |
// and expecting different return values, so disable calculation cacheing |
| 1095 |
\PhpOffice\PhpSpreadsheet\Calculation\Calculation::getInstance()->setCalculationCacheEnabled(FALSE); |
| 1096 |
|
| 1097 |
$saveFormat = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType(); |
| 1098 |
|
| 1099 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1100 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL |
| 1101 |
); |
| 1102 |
|
| 1103 |
$retVal = call_user_func_array( |
| 1104 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATE'], |
| 1105 |
[2008, 12, 31] |
| 1106 |
); |
| 1107 |
// $retVal = 39813.0 |
| 1108 |
|
| 1109 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1110 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC |
| 1111 |
); |
| 1112 |
|
| 1113 |
$retVal = call_user_func_array( |
| 1114 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATE'], |
| 1115 |
[2008, 12, 31] |
| 1116 |
); |
| 1117 |
// $retVal = 1230681600 |
| 1118 |
|
| 1119 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($saveFormat); |
| 1120 |
``` |
| 1121 |
|
| 1122 |
##### Notes |
| 1123 |
|
| 1124 |
There are no additional notes on this function |
| 1125 |
|
| 1126 |
#### DATEDIF |
| 1127 |
|
| 1128 |
The DATEDIF function computes the difference between two dates in a |
| 1129 |
variety of different intervals, such number of years, months, or days. |
| 1130 |
|
| 1131 |
##### Syntax |
| 1132 |
|
| 1133 |
DATEDIF(date1, date2 [, unit]) |
| 1134 |
|
| 1135 |
##### Parameters |
| 1136 |
|
| 1137 |
**date1** First Date. |
| 1138 |
|
| 1139 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1140 |
represented as a string. |
| 1141 |
|
| 1142 |
**date2** Second Date. |
| 1143 |
|
| 1144 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1145 |
represented as a string. |
| 1146 |
|
| 1147 |
**unit** The interval type to use for the calculation |
| 1148 |
|
| 1149 |
This is a string, comprising one of the values listed below: |
| 1150 |
|
| 1151 |
Unit | Meaning | Description |
| 1152 |
-----|---------------------------------|-------------------------------- |
| 1153 |
m | Months | Complete calendar months between the dates. |
| 1154 |
d | Days | Number of days between the dates. |
| 1155 |
y | Years | Complete calendar years between the dates. |
| 1156 |
ym | Months Excluding Years | Complete calendar months between the dates as if they were of the same year. |
| 1157 |
yd | Days Excluding Years | Complete calendar days between the dates as if they were of the same year. |
| 1158 |
md | Days Excluding Years And Months | Complete calendar days between the dates as if they were of the same month and same year. |
| 1159 |
|
| 1160 |
The unit value is not case sensitive, and defaults to `d`. |
| 1161 |
|
| 1162 |
##### Return Value |
| 1163 |
|
| 1164 |
**integer** An integer value that reflects the difference between the |
| 1165 |
two dates. |
| 1166 |
|
| 1167 |
This could be the number of full days, months or years between the two |
| 1168 |
dates, depending on the interval unit value passed into the function as |
| 1169 |
the third parameter. |
| 1170 |
|
| 1171 |
##### Examples |
| 1172 |
|
| 1173 |
``` php |
| 1174 |
$worksheet->setCellValue('A1', 'Year') |
| 1175 |
->setCellValue('A2', 'Month') |
| 1176 |
->setCellValue('A3', 'Day'); |
| 1177 |
|
| 1178 |
$worksheet->setCellValue('B1', 2001) |
| 1179 |
->setCellValue('C1', 2009) |
| 1180 |
->setCellValue('B2', 7) |
| 1181 |
->setCellValue('C2', 12) |
| 1182 |
->setCellValue('B3', 1) |
| 1183 |
->setCellValue('C3', 31); |
| 1184 |
|
| 1185 |
$worksheet->setCellValue('D1', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"d")') |
| 1186 |
->setCellValue('D2', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"m")') |
| 1187 |
->setCellValue('D3', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"y")') |
| 1188 |
->setCellValue('D4', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"ym")') |
| 1189 |
->setCellValue('D5', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"yd")') |
| 1190 |
->setCellValue('D6', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"md")'); |
| 1191 |
|
| 1192 |
$retVal = $worksheet->getCell('D1')->getCalculatedValue(); |
| 1193 |
// $retVal = 3105 |
| 1194 |
|
| 1195 |
$retVal = $worksheet->getCell('D2')->getCalculatedValue(); |
| 1196 |
// $retVal = 101 |
| 1197 |
|
| 1198 |
$retVal = $worksheet->getCell('D3')->getCalculatedValue(); |
| 1199 |
// $retVal = 8 |
| 1200 |
|
| 1201 |
$retVal = $worksheet->getCell('D4')->getCalculatedValue(); |
| 1202 |
// $retVal = 5 |
| 1203 |
|
| 1204 |
$retVal = $worksheet->getCell('D5')->getCalculatedValue(); |
| 1205 |
// $retVal = 183 |
| 1206 |
|
| 1207 |
$retVal = $worksheet->getCell('D6')->getCalculatedValue(); |
| 1208 |
// $retVal = 30 |
| 1209 |
``` |
| 1210 |
|
| 1211 |
``` php |
| 1212 |
$date1 = 1193317015; // PHP timestamp for 25-Oct-2007 |
| 1213 |
$date2 = 1449579415; // PHP timestamp for 8-Dec-2015 |
| 1214 |
|
| 1215 |
$retVal = call_user_func_array( |
| 1216 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'], |
| 1217 |
[$date1, $date2, 'd'] |
| 1218 |
); |
| 1219 |
// $retVal = 2966 |
| 1220 |
|
| 1221 |
$retVal = call_user_func_array( |
| 1222 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'], |
| 1223 |
[$date1, $date2, 'm'] |
| 1224 |
); |
| 1225 |
// $retVal = 97 |
| 1226 |
|
| 1227 |
$retVal = call_user_func_array( |
| 1228 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'], |
| 1229 |
[$date1, $date2, 'y'] |
| 1230 |
); |
| 1231 |
// $retVal = 8 |
| 1232 |
|
| 1233 |
$retVal = call_user_func_array( |
| 1234 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'], |
| 1235 |
[$date1, $date2, 'ym'] |
| 1236 |
); |
| 1237 |
// $retVal = 1 |
| 1238 |
|
| 1239 |
$retVal = call_user_func_array( |
| 1240 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'], |
| 1241 |
[$date1, $date2, 'yd'] |
| 1242 |
); |
| 1243 |
// $retVal = 44 |
| 1244 |
|
| 1245 |
$retVal = call_user_func_array( |
| 1246 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'], |
| 1247 |
[$date1, $date2, 'md'] |
| 1248 |
); |
| 1249 |
// $retVal = 13 |
| 1250 |
``` |
| 1251 |
|
| 1252 |
##### Notes |
| 1253 |
|
| 1254 |
If Date1 is later than Date2, DATEDIF will return a \#NUM! error. |
| 1255 |
|
| 1256 |
#### DATEVALUE |
| 1257 |
|
| 1258 |
The DATEVALUE function returns the date represented by a date formatted |
| 1259 |
as a text string. Use DATEVALUE to convert a date represented by text to |
| 1260 |
a serial number. |
| 1261 |
|
| 1262 |
##### Syntax |
| 1263 |
|
| 1264 |
DATEVALUE(dateString) |
| 1265 |
|
| 1266 |
##### Parameters |
| 1267 |
|
| 1268 |
**date** Date String. |
| 1269 |
|
| 1270 |
A string, representing a date value. |
| 1271 |
|
| 1272 |
##### Return Value |
| 1273 |
|
| 1274 |
**mixed** A date/time stamp that corresponds to the given date. |
| 1275 |
|
| 1276 |
This could be a PHP timestamp value (integer), a PHP `DateTime` object, |
| 1277 |
or an Excel timestamp value (real), depending on the value of |
| 1278 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`. |
| 1279 |
|
| 1280 |
##### Examples |
| 1281 |
|
| 1282 |
``` php |
| 1283 |
$worksheet->setCellValue('A1', 'Date String'); |
| 1284 |
->setCellValue('A2', '31-Dec-2008') |
| 1285 |
->setCellValue('A3', '31/12/2008') |
| 1286 |
->setCellValue('A4', '12-31-2008'); |
| 1287 |
|
| 1288 |
$worksheet->setCellValue('B2', '=DATEVALUE(A2)') |
| 1289 |
->setCellValue('B3', '=DATEVALUE(A3)') |
| 1290 |
->setCellValue('B4', '=DATEVALUE(A4)'); |
| 1291 |
|
| 1292 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1293 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL |
| 1294 |
); |
| 1295 |
|
| 1296 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1297 |
|
| 1298 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1299 |
|
| 1300 |
$retVal = $worksheet->getCell('B4')->getCalculatedValue(); |
| 1301 |
// $retVal = 39813.0 for all cases |
| 1302 |
``` |
| 1303 |
|
| 1304 |
``` php |
| 1305 |
// We're going to be calling the same cell calculation multiple times, |
| 1306 |
// and expecting different return values, so disable calculation cacheing |
| 1307 |
\PhpOffice\PhpSpreadsheet\Calculation\Calculation::getInstance()->setCalculationCacheEnabled(FALSE); |
| 1308 |
|
| 1309 |
$saveFormat = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType(); |
| 1310 |
|
| 1311 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1312 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL |
| 1313 |
); |
| 1314 |
|
| 1315 |
$retVal = call_user_func_array( |
| 1316 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEVALUE'], |
| 1317 |
['31-Dec-2008'] |
| 1318 |
); |
| 1319 |
// $retVal = 39813.0 |
| 1320 |
|
| 1321 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1322 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC |
| 1323 |
); |
| 1324 |
|
| 1325 |
$retVal = call_user_func_array( |
| 1326 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEVALUE'], |
| 1327 |
['31-Dec-2008'] |
| 1328 |
); |
| 1329 |
// $retVal = 1230681600 |
| 1330 |
|
| 1331 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($saveFormat); |
| 1332 |
``` |
| 1333 |
|
| 1334 |
##### Notes |
| 1335 |
|
| 1336 |
DATEVALUE uses the php `DateTime` object implementation of `strtotime()` |
| 1337 |
(which can handle a wider range of formats than the normal `strtotime()` |
| 1338 |
function), and it is also called for any date parameter passed to other |
| 1339 |
date functions (such as DATEDIF) when the parameter value is a string. |
| 1340 |
|
| 1341 |
**WARNING:-** PhpSpreadsheet accepts a wider range of date formats than |
| 1342 |
MS Excel, so it is entirely possible that Excel will return a \#VALUE! |
| 1343 |
error when passed a date string that it can’t interpret, while |
| 1344 |
PhpSpreadsheet is able to translate that same string into a correct date |
| 1345 |
value. |
| 1346 |
|
| 1347 |
Care should be taken in workbooks that use string formatted dates in |
| 1348 |
calculations when writing to Xls or Xlsx. |
| 1349 |
|
| 1350 |
#### DAY |
| 1351 |
|
| 1352 |
The DAY function returns the day of a date. The day is given as an |
| 1353 |
integer ranging from 1 to 31. |
| 1354 |
|
| 1355 |
##### Syntax |
| 1356 |
|
| 1357 |
DAY(datetime) |
| 1358 |
|
| 1359 |
##### Parameters |
| 1360 |
|
| 1361 |
**datetime** Date. |
| 1362 |
|
| 1363 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1364 |
represented as a string. |
| 1365 |
|
| 1366 |
##### Return Value |
| 1367 |
|
| 1368 |
**integer** An integer value that reflects the day of the month. |
| 1369 |
|
| 1370 |
This is an integer ranging from 1 to 31. |
| 1371 |
|
| 1372 |
##### Examples |
| 1373 |
|
| 1374 |
``` php |
| 1375 |
$worksheet->setCellValue('A1', 'Date String') |
| 1376 |
->setCellValue('A2', '31-Dec-2008') |
| 1377 |
->setCellValue('A3', '14-Feb-2008'); |
| 1378 |
|
| 1379 |
$worksheet->setCellValue('B2', '=DAY(A2)') |
| 1380 |
->setCellValue('B3', '=DAY(A3)'); |
| 1381 |
|
| 1382 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1383 |
// $retVal = 31 |
| 1384 |
|
| 1385 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1386 |
// $retVal = 14 |
| 1387 |
``` |
| 1388 |
|
| 1389 |
``` php |
| 1390 |
$retVal = call_user_func_array( |
| 1391 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYOFMONTH'], |
| 1392 |
['25-Dec-2008'] |
| 1393 |
); |
| 1394 |
// $retVal = 25 |
| 1395 |
``` |
| 1396 |
|
| 1397 |
##### Notes |
| 1398 |
|
| 1399 |
Note that the PhpSpreadsheet function is |
| 1400 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::DAYOFMONTH()` when the |
| 1401 |
method is called statically. |
| 1402 |
|
| 1403 |
#### DAYS360 |
| 1404 |
|
| 1405 |
The DAYS360 function computes the difference between two dates based on |
| 1406 |
a 360 day year (12 equal periods of 30 days each) used by some |
| 1407 |
accounting systems. |
| 1408 |
|
| 1409 |
##### Syntax |
| 1410 |
|
| 1411 |
DAYS360(date1, date2 [, method]) |
| 1412 |
|
| 1413 |
#### Parameters |
| 1414 |
|
| 1415 |
**date1** First Date. |
| 1416 |
|
| 1417 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1418 |
represented as a string. |
| 1419 |
|
| 1420 |
**date2** Second Date. |
| 1421 |
|
| 1422 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1423 |
represented as a string. |
| 1424 |
|
| 1425 |
**method** A boolean flag (TRUE or FALSE) |
| 1426 |
|
| 1427 |
This is a flag that determines which method to use in the calculation, |
| 1428 |
based on the values listed below: |
| 1429 |
|
| 1430 |
method | Description |
| 1431 |
-------|------------ |
| 1432 |
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. |
| 1433 |
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. |
| 1434 |
|
| 1435 |
The method value defaults to FALSE. |
| 1436 |
|
| 1437 |
##### Return Value |
| 1438 |
|
| 1439 |
**integer** An integer value that reflects the difference between the |
| 1440 |
two dates. |
| 1441 |
|
| 1442 |
This is the number of full days between the two dates, based on a 360 |
| 1443 |
day year. |
| 1444 |
|
| 1445 |
##### Examples |
| 1446 |
|
| 1447 |
``` php |
| 1448 |
$worksheet->setCellValue('B1', 'Start Date') |
| 1449 |
->setCellValue('C1', 'End Date') |
| 1450 |
->setCellValue('A2', 'Year') |
| 1451 |
->setCellValue('A3', 'Month') |
| 1452 |
->setCellValue('A4', 'Day'); |
| 1453 |
|
| 1454 |
$worksheet->setCellValue('B2', 2003) |
| 1455 |
->setCellValue('B3', 2) |
| 1456 |
->setCellValue('B4', 3); |
| 1457 |
|
| 1458 |
$worksheet->setCellValue('C2', 2007) |
| 1459 |
->setCellValue('C3', 5) |
| 1460 |
->setCellValue('C4', 31); |
| 1461 |
|
| 1462 |
$worksheet->setCellValue('E2', '=DAYS360(DATE(B2,B3,B4),DATE(C2,C3,C4))') |
| 1463 |
->setCellValue('E4', '=DAYS360(DATE(B2,B3,B4),DATE(C2,C3,C4),FALSE)'); |
| 1464 |
|
| 1465 |
$retVal = $worksheet->getCell('E2')->getCalculatedValue(); |
| 1466 |
// $retVal = 1558 |
| 1467 |
|
| 1468 |
$retVal = $worksheet->getCell('E4')->getCalculatedValue(); |
| 1469 |
// $retVal = 1557 |
| 1470 |
``` |
| 1471 |
|
| 1472 |
``` php |
| 1473 |
$date1 = 37655.0; // Excel timestamp for 25-Oct-2007 |
| 1474 |
$date2 = 39233.0; // Excel timestamp for 8-Dec-2015 |
| 1475 |
|
| 1476 |
$retVal = call_user_func_array( |
| 1477 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYS360'], |
| 1478 |
[$date1, $date2] |
| 1479 |
); |
| 1480 |
// $retVal = 1558 |
| 1481 |
|
| 1482 |
$retVal = call_user_func_array( |
| 1483 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYS360'], |
| 1484 |
[$date1, $date2, TRUE] |
| 1485 |
); |
| 1486 |
// $retVal = 1557 |
| 1487 |
``` |
| 1488 |
|
| 1489 |
##### Notes |
| 1490 |
|
| 1491 |
**WARNING:-** This function does not currently work with the Xls Writer |
| 1492 |
when a PHP Boolean is used for the third (optional) parameter (as shown |
| 1493 |
in the example above), and the writer will generate and error. It will |
| 1494 |
work if a numeric 0 or 1 is used for the method parameter; or if the |
| 1495 |
Excel `TRUE()` and `FALSE()` functions are used instead. |
| 1496 |
|
| 1497 |
#### EDATE |
| 1498 |
|
| 1499 |
The EDATE function returns an Excel timestamp or a PHP timestamp or `DateTime` |
| 1500 |
object representing the date that is the indicated number of months |
| 1501 |
before or after a specified date (the start\_date). Use EDATE to |
| 1502 |
calculate maturity dates or due dates that fall on the same day of the |
| 1503 |
month as the date of issue. |
| 1504 |
|
| 1505 |
##### Syntax |
| 1506 |
|
| 1507 |
EDATE(baseDate, months) |
| 1508 |
|
| 1509 |
##### Parameters |
| 1510 |
|
| 1511 |
**baseDate** Start Date. |
| 1512 |
|
| 1513 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1514 |
represented as a string. |
| 1515 |
|
| 1516 |
**months** Number of months to add. |
| 1517 |
|
| 1518 |
An integer value indicating the number of months before or after |
| 1519 |
baseDate. A positive value for months yields a future date; a negative |
| 1520 |
value yields a past date. |
| 1521 |
|
| 1522 |
##### Return Value |
| 1523 |
|
| 1524 |
**mixed** A date/time stamp that corresponds to the basedate + months. |
| 1525 |
|
| 1526 |
This could be a PHP timestamp value (integer), a PHP `DateTime` object, |
| 1527 |
or an Excel timestamp value (real), depending on the value of |
| 1528 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`. |
| 1529 |
|
| 1530 |
##### Examples |
| 1531 |
|
| 1532 |
``` php |
| 1533 |
$worksheet->setCellValue('A1', 'Date String') |
| 1534 |
->setCellValue('A2', '1-Jan-2008') |
| 1535 |
->setCellValue('A3', '29-Feb-2008'); |
| 1536 |
|
| 1537 |
$worksheet->setCellValue('B2', '=EDATE(A2,5)') |
| 1538 |
->setCellValue('B3', '=EDATE(A3,-12)'); |
| 1539 |
|
| 1540 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1541 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL |
| 1542 |
); |
| 1543 |
|
| 1544 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1545 |
// $retVal = 39600.0 (1-Jun-2008) |
| 1546 |
|
| 1547 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1548 |
// $retVal = 39141.0 (28-Feb-2007) |
| 1549 |
``` |
| 1550 |
|
| 1551 |
``` php |
| 1552 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1553 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL |
| 1554 |
); |
| 1555 |
|
| 1556 |
$retVal = call_user_func_array( |
| 1557 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'EDATE'], |
| 1558 |
['31-Oct-2008', 25] |
| 1559 |
); |
| 1560 |
// $retVal = 40512.0 (30-Nov-2010) |
| 1561 |
``` |
| 1562 |
|
| 1563 |
###### Notes |
| 1564 |
|
| 1565 |
**WARNING:-** This function is currently not supported by the Xls Writer |
| 1566 |
because it is not a standard function within Excel 5, but an add-in from |
| 1567 |
the Analysis ToolPak. |
| 1568 |
|
| 1569 |
#### EOMONTH |
| 1570 |
|
| 1571 |
The EOMONTH function returns an Excel timestamp or a PHP timestamp or |
| 1572 |
`DateTime` object representing the date of the last day of the month that is |
| 1573 |
the indicated number of months before or after a specified date (the |
| 1574 |
start\_date). Use EOMONTH to calculate maturity dates or due dates that |
| 1575 |
fall on the last day of the month. |
| 1576 |
|
| 1577 |
##### Syntax |
| 1578 |
|
| 1579 |
EOMONTH(baseDate, months) |
| 1580 |
|
| 1581 |
##### Parameters |
| 1582 |
|
| 1583 |
**baseDate** Start Date. |
| 1584 |
|
| 1585 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1586 |
represented as a string. |
| 1587 |
|
| 1588 |
**months** Number of months to add. |
| 1589 |
|
| 1590 |
An integer value indicating the number of months before or after |
| 1591 |
baseDate. A positive value for months yields a future date; a negative |
| 1592 |
value yields a past date. |
| 1593 |
|
| 1594 |
##### Return Value |
| 1595 |
|
| 1596 |
**mixed** A date/time stamp that corresponds to the last day of basedate |
| 1597 |
+ months. |
| 1598 |
|
| 1599 |
This could be a PHP timestamp value (integer), a PHP `DateTime` object, |
| 1600 |
or an Excel timestamp value (real), depending on the value of |
| 1601 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`. |
| 1602 |
|
| 1603 |
##### Examples |
| 1604 |
|
| 1605 |
``` php |
| 1606 |
$worksheet->setCellValue('A1', 'Date String') |
| 1607 |
->setCellValue('A2', '1-Jan-2000') |
| 1608 |
->setCellValue('A3', '14-Feb-2009'); |
| 1609 |
|
| 1610 |
$worksheet->setCellValue('B2', '=EOMONTH(A2,5)') |
| 1611 |
->setCellValue('B3', '=EOMONTH(A3,-12)'); |
| 1612 |
|
| 1613 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL); |
| 1614 |
|
| 1615 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1616 |
// $retVal = 39629.0 (30-Jun-2008) |
| 1617 |
|
| 1618 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1619 |
// $retVal = 39507.0 (29-Feb-2008) |
| 1620 |
``` |
| 1621 |
|
| 1622 |
``` php |
| 1623 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType( |
| 1624 |
\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL |
| 1625 |
); |
| 1626 |
|
| 1627 |
$retVal = call_user_func_array( |
| 1628 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'EOMONTH'], |
| 1629 |
['31-Oct-2008', 13] |
| 1630 |
); |
| 1631 |
// $retVal = 40147.0 (30-Nov-2010) |
| 1632 |
``` |
| 1633 |
|
| 1634 |
##### Notes |
| 1635 |
|
| 1636 |
**WARNING:-** This function is currently not supported by the Xls Writer |
| 1637 |
because it is not a standard function within Excel 5, but an add-in from |
| 1638 |
the Analysis ToolPak. |
| 1639 |
|
| 1640 |
#### HOUR |
| 1641 |
|
| 1642 |
The HOUR function returns the hour of a time value. The hour is given as |
| 1643 |
an integer, ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.). |
| 1644 |
|
| 1645 |
##### Syntax |
| 1646 |
|
| 1647 |
HOUR(datetime) |
| 1648 |
|
| 1649 |
##### Parameters |
| 1650 |
|
| 1651 |
**datetime** Time. |
| 1652 |
|
| 1653 |
An Excel date/time value, PHP date timestamp, PHP `DateTime` object, or a |
| 1654 |
date/time represented as a string. |
| 1655 |
|
| 1656 |
##### Return Value |
| 1657 |
|
| 1658 |
**integer** An integer value that reflects the hour of the day. |
| 1659 |
|
| 1660 |
This is an integer ranging from 0 to 23. |
| 1661 |
|
| 1662 |
##### Examples |
| 1663 |
|
| 1664 |
``` php |
| 1665 |
$worksheet->setCellValue('A1', 'Time String') |
| 1666 |
->setCellValue('A2', '31-Dec-2008 17:30') |
| 1667 |
->setCellValue('A3', '14-Feb-2008 4:20 AM') |
| 1668 |
->setCellValue('A4', '14-Feb-2008 4:20 PM'); |
| 1669 |
|
| 1670 |
$worksheet->setCellValue('B2', '=HOUR(A2)') |
| 1671 |
->setCellValue('B3', '=HOUR(A3)') |
| 1672 |
->setCellValue('B4', '=HOUR(A4)'); |
| 1673 |
|
| 1674 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1675 |
// $retVal = 17 |
| 1676 |
|
| 1677 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1678 |
// $retVal = 4 |
| 1679 |
|
| 1680 |
$retVal = $worksheet->getCell('B4')->getCalculatedValue(); |
| 1681 |
// $retVal = 16 |
| 1682 |
``` |
| 1683 |
|
| 1684 |
``` php |
| 1685 |
$retVal = call_user_func_array( |
| 1686 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'HOUROFDAY'], |
| 1687 |
['09:30'] |
| 1688 |
); |
| 1689 |
// $retVal = 9 |
| 1690 |
``` |
| 1691 |
|
| 1692 |
##### Notes |
| 1693 |
|
| 1694 |
Note that the PhpSpreadsheet function is |
| 1695 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::HOUROFDAY()` when the |
| 1696 |
method is called statically. |
| 1697 |
|
| 1698 |
#### MINUTE |
| 1699 |
|
| 1700 |
The MINUTE function returns the minutes of a time value. The minute is |
| 1701 |
given as an integer, ranging from 0 to 59. |
| 1702 |
|
| 1703 |
##### Syntax |
| 1704 |
|
| 1705 |
MINUTE(datetime) |
| 1706 |
|
| 1707 |
##### Parameters |
| 1708 |
|
| 1709 |
**datetime** Time. |
| 1710 |
|
| 1711 |
An Excel date/time value, PHP date timestamp, PHP `DateTime` object, or a |
| 1712 |
date/time represented as a string. |
| 1713 |
|
| 1714 |
##### Return Value |
| 1715 |
|
| 1716 |
**integer** An integer value that reflects the minutes within the hour. |
| 1717 |
|
| 1718 |
This is an integer ranging from 0 to 59. |
| 1719 |
|
| 1720 |
##### Examples |
| 1721 |
|
| 1722 |
``` php |
| 1723 |
$worksheet->setCellValue('A1', 'Time String') |
| 1724 |
->setCellValue('A2', '31-Dec-2008 17:30') |
| 1725 |
->setCellValue('A3', '14-Feb-2008 4:20 AM') |
| 1726 |
->setCellValue('A4', '14-Feb-2008 4:45 PM'); |
| 1727 |
|
| 1728 |
$worksheet->setCellValue('B2', '=MINUTE(A2)') |
| 1729 |
->setCellValue('B3', '=MINUTE(A3)') |
| 1730 |
->setCellValue('B4', '=MINUTE(A4)'); |
| 1731 |
|
| 1732 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1733 |
// $retVal = 30 |
| 1734 |
|
| 1735 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1736 |
// $retVal = 20 |
| 1737 |
|
| 1738 |
$retVal = $worksheet->getCell('B4')->getCalculatedValue(); |
| 1739 |
// $retVal = 45 |
| 1740 |
``` |
| 1741 |
|
| 1742 |
``` php |
| 1743 |
$retVal = call_user_func_array( |
| 1744 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'MINUTE'], |
| 1745 |
['09:30'] |
| 1746 |
); |
| 1747 |
// $retVal = 30 |
| 1748 |
``` |
| 1749 |
|
| 1750 |
##### Notes |
| 1751 |
|
| 1752 |
Note that the PhpSpreadsheet function is |
| 1753 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::MINUTE()` when the |
| 1754 |
method is called statically. |
| 1755 |
|
| 1756 |
#### MONTH |
| 1757 |
|
| 1758 |
The MONTH function returns the month of a date. The month is given as an |
| 1759 |
integer ranging from 1 to 12. |
| 1760 |
|
| 1761 |
##### Syntax |
| 1762 |
|
| 1763 |
MONTH(datetime) |
| 1764 |
|
| 1765 |
##### Parameters |
| 1766 |
|
| 1767 |
**datetime** Date. |
| 1768 |
|
| 1769 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1770 |
represented as a string. |
| 1771 |
|
| 1772 |
##### Return Value |
| 1773 |
|
| 1774 |
**integer** An integer value that reflects the month of the year. |
| 1775 |
|
| 1776 |
This is an integer ranging from 1 to 12. |
| 1777 |
|
| 1778 |
##### Examples |
| 1779 |
|
| 1780 |
``` php |
| 1781 |
$worksheet->setCellValue('A1', 'Date String'); |
| 1782 |
$worksheet->setCellValue('A2', '31-Dec-2008'); |
| 1783 |
$worksheet->setCellValue('A3', '14-Feb-2008'); |
| 1784 |
|
| 1785 |
$worksheet->setCellValue('B2', '=MONTH(A2)'); |
| 1786 |
$worksheet->setCellValue('B3', '=MONTH(A3)'); |
| 1787 |
|
| 1788 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1789 |
// $retVal = 12 |
| 1790 |
|
| 1791 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1792 |
// $retVal = 2 |
| 1793 |
``` |
| 1794 |
|
| 1795 |
``` php |
| 1796 |
$retVal = call_user_func_array( |
| 1797 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'MONTHOFYEAR'], |
| 1798 |
['14-July-2008'] |
| 1799 |
); |
| 1800 |
// $retVal = 7 |
| 1801 |
``` |
| 1802 |
|
| 1803 |
#### Notes |
| 1804 |
|
| 1805 |
Note that the PhpSpreadsheet function is |
| 1806 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::MONTHOFYEAR()` when the |
| 1807 |
method is called statically. |
| 1808 |
|
| 1809 |
#### NETWORKDAYS |
| 1810 |
|
| 1811 |
The NETWORKDAYS function returns the number of whole working days |
| 1812 |
between a *start date* and an *end date*. Working days exclude weekends |
| 1813 |
and any dates identified in *holidays*. Use NETWORKDAYS to calculate |
| 1814 |
employee benefits that accrue based on the number of days worked during |
| 1815 |
a specific term. |
| 1816 |
|
| 1817 |
##### Syntax |
| 1818 |
|
| 1819 |
NETWORKDAYS(startDate, endDate [, holidays]) |
| 1820 |
|
| 1821 |
##### Parameters |
| 1822 |
|
| 1823 |
**startDate** Start Date of the period. |
| 1824 |
|
| 1825 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1826 |
represented as a string. |
| 1827 |
|
| 1828 |
**endDate** End Date of the period. |
| 1829 |
|
| 1830 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1831 |
represented as a string. |
| 1832 |
|
| 1833 |
**holidays** Optional array of Holiday dates. |
| 1834 |
|
| 1835 |
An optional range of one or more dates to exclude from the working |
| 1836 |
calendar, such as state and federal holidays and floating holidays. |
| 1837 |
|
| 1838 |
The list can be either a range of cells that contains the dates or an |
| 1839 |
array constant of Excel date values, PHP date timestamps, PHP date |
| 1840 |
objects, or dates represented as strings. |
| 1841 |
|
| 1842 |
##### Return Value |
| 1843 |
|
| 1844 |
**integer** Number of working days. |
| 1845 |
|
| 1846 |
The number of working days between startDate and endDate. |
| 1847 |
|
| 1848 |
##### Examples |
| 1849 |
|
| 1850 |
``` php |
| 1851 |
``` |
| 1852 |
|
| 1853 |
``` php |
| 1854 |
``` |
| 1855 |
|
| 1856 |
##### Notes |
| 1857 |
|
| 1858 |
There are no additional notes on this function |
| 1859 |
|
| 1860 |
#### NOW |
| 1861 |
|
| 1862 |
The NOW function returns the current date and time. |
| 1863 |
|
| 1864 |
##### Syntax |
| 1865 |
|
| 1866 |
NOW() |
| 1867 |
|
| 1868 |
##### Parameters |
| 1869 |
|
| 1870 |
There are no parameters for the `NOW()` function. |
| 1871 |
|
| 1872 |
##### Return Value |
| 1873 |
|
| 1874 |
**mixed** A date/time stamp that corresponds to the current date and |
| 1875 |
time. |
| 1876 |
|
| 1877 |
This could be a PHP timestamp value (integer), a PHP `DateTime` object, |
| 1878 |
or an Excel timestamp value (real), depending on the value of |
| 1879 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()`. |
| 1880 |
|
| 1881 |
##### Examples |
| 1882 |
|
| 1883 |
``` php |
| 1884 |
``` |
| 1885 |
|
| 1886 |
``` php |
| 1887 |
``` |
| 1888 |
|
| 1889 |
##### Notes |
| 1890 |
|
| 1891 |
Note that the PhpSpreadsheet function is |
| 1892 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::DATETIMENOW()` when the |
| 1893 |
method is called statically. |
| 1894 |
|
| 1895 |
#### SECOND |
| 1896 |
|
| 1897 |
The SECOND function returns the seconds of a time value. The second is |
| 1898 |
given as an integer, ranging from 0 to 59. |
| 1899 |
|
| 1900 |
##### Syntax |
| 1901 |
|
| 1902 |
SECOND(datetime) |
| 1903 |
|
| 1904 |
##### Parameters |
| 1905 |
|
| 1906 |
**datetime** Time. |
| 1907 |
|
| 1908 |
An Excel date/time value, PHP date timestamp, PHP `DateTime` object, or a |
| 1909 |
date/time represented as a string. |
| 1910 |
|
| 1911 |
##### Return Value |
| 1912 |
|
| 1913 |
**integer** An integer value that reflects the seconds within the |
| 1914 |
minute. |
| 1915 |
|
| 1916 |
This is an integer ranging from 0 to 59. |
| 1917 |
|
| 1918 |
##### Examples |
| 1919 |
|
| 1920 |
``` php |
| 1921 |
$worksheet->setCellValue('A1', 'Time String') |
| 1922 |
->setCellValue('A2', '31-Dec-2008 17:30:20') |
| 1923 |
->setCellValue('A3', '14-Feb-2008 4:20 AM') |
| 1924 |
->setCellValue('A4', '14-Feb-2008 4:45:59 PM'); |
| 1925 |
|
| 1926 |
$worksheet->setCellValue('B2', '=SECOND(A2)') |
| 1927 |
->setCellValue('B3', '=SECOND(A3)'); |
| 1928 |
->setCellValue('B4', '=SECOND(A4)'); |
| 1929 |
|
| 1930 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 1931 |
// $retVal = 20 |
| 1932 |
|
| 1933 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 1934 |
// $retVal = 0 |
| 1935 |
|
| 1936 |
$retVal = $worksheet->getCell('B4')->getCalculatedValue(); |
| 1937 |
// $retVal = 59 |
| 1938 |
``` |
| 1939 |
|
| 1940 |
``` php |
| 1941 |
$retVal = call_user_func_array( |
| 1942 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'SECOND'], |
| 1943 |
['09:30:17'] |
| 1944 |
); |
| 1945 |
// $retVal = 17 |
| 1946 |
``` |
| 1947 |
|
| 1948 |
##### Notes |
| 1949 |
|
| 1950 |
Note that the PhpSpreadsheet function is |
| 1951 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::SECOND()` when the |
| 1952 |
method is called statically. |
| 1953 |
|
| 1954 |
#### TIME |
| 1955 |
|
| 1956 |
Not yet documented. |
| 1957 |
|
| 1958 |
#### TIMEVALUE |
| 1959 |
|
| 1960 |
Not yet documented. |
| 1961 |
|
| 1962 |
#### TODAY |
| 1963 |
|
| 1964 |
Not yet documented. |
| 1965 |
|
| 1966 |
#### WEEKDAY |
| 1967 |
|
| 1968 |
The WEEKDAY function returns the day of the week for a given date. The |
| 1969 |
day is given as an integer ranging from 1 to 7, although this can be |
| 1970 |
modified to return a value between 0 and 6. |
| 1971 |
|
| 1972 |
##### Syntax |
| 1973 |
|
| 1974 |
WEEKDAY(datetime [, method]) |
| 1975 |
|
| 1976 |
##### Parameters |
| 1977 |
|
| 1978 |
**datetime** Date. |
| 1979 |
|
| 1980 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 1981 |
represented as a string. |
| 1982 |
|
| 1983 |
**method** An integer flag (values 0, 1 or 2) |
| 1984 |
|
| 1985 |
This is a flag that determines which method to use in the calculation, |
| 1986 |
based on the values listed below: |
| 1987 |
|
| 1988 |
method | Description |
| 1989 |
:-----:|------------------------------------------ |
| 1990 |
0 | Returns 1 (Sunday) through 7 (Saturday). |
| 1991 |
1 | Returns 1 (Monday) through 7 (Sunday). |
| 1992 |
2 | Returns 0 (Monday) through 6 (Sunday). |
| 1993 |
|
| 1994 |
The method value defaults to 1. |
| 1995 |
|
| 1996 |
##### Return Value |
| 1997 |
|
| 1998 |
**integer** An integer value that reflects the day of the week. |
| 1999 |
|
| 2000 |
This is an integer ranging from 1 to 7, or 0 to 6, depending on the |
| 2001 |
value of method. |
| 2002 |
|
| 2003 |
##### Examples |
| 2004 |
|
| 2005 |
``` php |
| 2006 |
$worksheet->setCellValue('A1', 'Date String') |
| 2007 |
->setCellValue('A2', '31-Dec-2008') |
| 2008 |
->setCellValue('A3', '14-Feb-2008'); |
| 2009 |
|
| 2010 |
$worksheet->setCellValue('B2', '=WEEKDAY(A2)') |
| 2011 |
->setCellValue('B3', '=WEEKDAY(A3,0)') |
| 2012 |
->setCellValue('B4', '=WEEKDAY(A3,2)'); |
| 2013 |
|
| 2014 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 2015 |
// $retVal = 12 |
| 2016 |
|
| 2017 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 2018 |
// $retVal = 2 |
| 2019 |
|
| 2020 |
$retVal = $worksheet->getCell('B4')->getCalculatedValue(); |
| 2021 |
// $retVal = 2 |
| 2022 |
``` |
| 2023 |
|
| 2024 |
``` php |
| 2025 |
$retVal = call_user_func_array( |
| 2026 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'WEEKDAY'], |
| 2027 |
['14-July-2008'] |
| 2028 |
); |
| 2029 |
// $retVal = 7 |
| 2030 |
``` |
| 2031 |
|
| 2032 |
##### Notes |
| 2033 |
|
| 2034 |
Note that the PhpSpreadsheet function is |
| 2035 |
`\PhpOffice\PhpSpreadsheet\Calculation\Functions::WEEKDAY()` when the |
| 2036 |
method is called statically. |
| 2037 |
|
| 2038 |
#### WEEKNUM |
| 2039 |
|
| 2040 |
Not yet documented. |
| 2041 |
|
| 2042 |
#### WORKDAY |
| 2043 |
|
| 2044 |
Not yet documented. |
| 2045 |
|
| 2046 |
#### YEAR |
| 2047 |
|
| 2048 |
The YEAR function returns the year of a date. |
| 2049 |
|
| 2050 |
##### Syntax |
| 2051 |
|
| 2052 |
YEAR(datetime) |
| 2053 |
|
| 2054 |
##### Parameters |
| 2055 |
|
| 2056 |
**datetime** Date. |
| 2057 |
|
| 2058 |
An Excel date value, PHP date timestamp, PHP `DateTime` object, or a date |
| 2059 |
represented as a string. |
| 2060 |
|
| 2061 |
##### Return Value |
| 2062 |
|
| 2063 |
**integer** An integer value that reflects the month of the year. |
| 2064 |
|
| 2065 |
This is an integer year value. |
| 2066 |
|
| 2067 |
##### Examples |
| 2068 |
|
| 2069 |
``` php |
| 2070 |
$worksheet->setCellValue('A1', 'Date String') |
| 2071 |
->setCellValue('A2', '17-Jul-1982') |
| 2072 |
->setCellValue('A3', '16-Apr-2009'); |
| 2073 |
|
| 2074 |
$worksheet->setCellValue('B2', '=YEAR(A2)') |
| 2075 |
->setCellValue('B3', '=YEAR(A3)'); |
| 2076 |
|
| 2077 |
$retVal = $worksheet->getCell('B2')->getCalculatedValue(); |
| 2078 |
// $retVal = 1982 |
| 2079 |
|
| 2080 |
$retVal = $worksheet->getCell('B3')->getCalculatedValue(); |
| 2081 |
// $retVal = 2009 |
| 2082 |
``` |
| 2083 |
|
| 2084 |
``` php |
| 2085 |
$retVal = call_user_func_array( |
| 2086 |
['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'YEAR'], |
| 2087 |
['14-July-2001'] |
| 2088 |
); |
| 2089 |
// $retVal = 2001 |
| 2090 |
``` |
| 2091 |
|
| 2092 |
##### Notes |
| 2093 |
|
| 2094 |
There are no additional notes on this function |
| 2095 |
|
| 2096 |
### YEARFRAC |
| 2097 |
|
| 2098 |
Not yet documented. |
| 2099 |
|