| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
| 4 |
|
| 5 |
use DateTimeInterface; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 9 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
| 10 |
|
| 11 |
class Days |
| 12 |
{ |
| 13 |
use ArrayEnabled; |
| 14 |
|
| 15 |
/** |
| 16 |
* DAYS. |
| 17 |
* |
| 18 |
* Returns the number of days between two dates |
| 19 |
* |
| 20 |
* Excel Function: |
| 21 |
* DAYS(endDate, startDate) |
| 22 |
* |
| 23 |
* @param array<mixed>|DateTimeInterface|float|int|string $endDate Excel date serial value (float), |
| 24 |
* PHP date timestamp (integer), PHP DateTime object, or a standard date string |
| 25 |
* Or can be an array of date values |
| 26 |
* @param array<mixed>|DateTimeInterface|float|int|string $startDate Excel date serial value (float), |
| 27 |
* PHP date timestamp (integer), PHP DateTime object, or a standard date string |
| 28 |
* Or can be an array of date values |
| 29 |
* |
| 30 |
* @return array<mixed>|int|string Number of days between start date and end date or an error |
| 31 |
* If an array of values is passed for the $startDate or $endDays,arguments, then the returned result |
| 32 |
* will also be an array with matching dimensions |
| 33 |
*/ |
| 34 |
public static function between($endDate, $startDate) |
| 35 |
{ |
| 36 |
if (is_array($endDate) || is_array($startDate)) { |
| 37 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $endDate, $startDate); |
| 38 |
} |
| 39 |
|
| 40 |
try { |
| 41 |
$startDate = Helpers::getDateValue($startDate); |
| 42 |
$endDate = Helpers::getDateValue($endDate); |
| 43 |
} catch (Exception $e) { |
| 44 |
return $e->getMessage(); |
| 45 |
} |
| 46 |
|
| 47 |
// Execute function |
| 48 |
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate); |
| 49 |
$PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate); |
| 50 |
|
| 51 |
$days = ExcelError::VALUE(); |
| 52 |
$diff = $PHPStartDateObject->diff($PHPEndDateObject); |
| 53 |
$days = $diff->days; |
| 54 |
if ($diff->invert) { |
| 55 |
$days = -$days; |
| 56 |
} |
| 57 |
|
| 58 |
return $days; |
| 59 |
} |
| 60 |
} |
| 61 |
|