Skip to content

Commit

Permalink
Added the DAYS() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Biser Antonov authored and PowerKiKi committed Oct 21, 2018
1 parent a1e8c84 commit 2c981e4
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Add the DAYS() function - [#594](https://github.com/PHPOffice/PhpSpreadsheet/pull/594)

### Fixed

- Sheet title can contain exclamation mark - [#325](https://github.com/PHPOffice/PhpSpreadsheet/issues/325)
Expand Down
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ class Calculation
'functionCall' => [DateTime::class, 'DAYOFMONTH'],
'argumentCount' => '1',
],
'DAYS' => [
'category' => Category::CATEGORY_DATE_AND_TIME,
'functionCall' => [DateTime::class, 'DAYS'],
'argumentCount' => '2',
],
'DAYS360' => [
'category' => Category::CATEGORY_DATE_AND_TIME,
'functionCall' => [DateTime::class, 'DAYS360'],
Expand Down
48 changes: 47 additions & 1 deletion src/PhpSpreadsheet/Calculation/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function getDateValue($dateValue)
(Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC)) {
return Functions::VALUE();
}
if ((is_object($dateValue)) && ($dateValue instanceof \DateTimeImmutable)) {
if ((is_object($dateValue)) && ($dateValue instanceof \DateTimeInterface)) {
$dateValue = Date::PHPToExcel($dateValue);
} else {
$saveReturnDateType = Functions::getReturnDateType();
Expand Down Expand Up @@ -764,6 +764,52 @@ public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D')
return $retVal;
}

/**
* DAYS.
*
* Returns the number of days between two dates
*
* Excel Function:
* DAYS(endDate, startDate)
*
* @category Date/Time Functions
*
* @param \DateTimeImmutable|float|int|string $endDate Excel date serial value (float),
* PHP date timestamp (integer), PHP DateTime object, or a standard date string
* @param \DateTimeImmutable|float|int|string $startDate Excel date serial value (float),
* PHP date timestamp (integer), PHP DateTime object, or a standard date string
*
* @return int|string Number of days between start date and end date or an error
*/
public static function DAYS($endDate = 0, $startDate = 0)
{
$startDate = Functions::flattenSingleValue($startDate);
$endDate = Functions::flattenSingleValue($endDate);

$startDate = self::getDateValue($startDate);
if (is_string($startDate)) {
return Functions::VALUE();
}

$endDate = self::getDateValue($endDate);
if (is_string($endDate)) {
return Functions::VALUE();
}

// Execute function
$PHPStartDateObject = Date::excelToDateTimeObject($startDate);
$PHPEndDateObject = Date::excelToDateTimeObject($endDate);

$diff = $PHPStartDateObject->diff($PHPEndDateObject);
$days = $diff->days;

if ($diff->invert) {
$days = -$days;
}

return $days;
}

/**
* DAYS360.
*
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Calculation/functionlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ DATEDIF
DATEVALUE
DAVERAGE
DAY
DAYS
DAYS360
DB
DCOUNT
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,22 @@ public function providerDATEDIF()
return require 'data/Calculation/DateTime/DATEDIF.php';
}

/**
* @dataProvider providerDAYS
*
* @param mixed $expectedResult
*/
public function testDAYS($expectedResult, ...$args)
{
$result = DateTime::DAYS(...$args);
self::assertEquals($expectedResult, $result, null, 1E-8);
}

public function providerDAYS()
{
return require 'data/Calculation/DateTime/DAYS.php';
}

/**
* @dataProvider providerDAYS360
*
Expand Down
99 changes: 99 additions & 0 deletions tests/data/Calculation/DateTime/DAYS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

return [
[
'#VALUE!',
'2007-1-10',
'ABC',
],
[
'#VALUE!',
'DEF',
'2007-1-1',
],
[
9,
'2007-1-10',
'2007-1-1',
],
[
364,
'2007-12-31',
'2007-1-1',
],
[
547,
'2008-7-1',
'2007-1-1',
],
[
30,
'2007-1-31',
'2007-1-1',
],
[
31,
'2007-2-1',
'2007-1-1',
],
[
58,
'2007-2-28',
'2007-1-1',
],
[
1,
'2007-2-1',
'2007-1-31',
],
[
29,
'2007-3-1',
'2007-1-31',
],
[
59,
'2007-3-31',
'2007-1-31',
],
[
244,
'2008-9-1',
'2008-1-1',
],
[
425,
'2008-4-1',
'2007-2-1',
],
[
17358,
'2008-6-28',
'1960-12-19',
],
[
9335,
'2008-6-28',
'1982-12-7',
],
[
32,
'2000-3-31',
'2000-2-28',
],
[
31,
'2000-3-31',
'2000-2-29',
],
[
31,
new \DateTime('2000-3-31'),
new \DateTimeImmutable('2000-2-29'),
],
[
31,
36616,
36585,
],
];

0 comments on commit 2c981e4

Please sign in to comment.