Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the DAYS() function #594

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,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
43 changes: 43 additions & 0 deletions src/PhpSpreadsheet/Calculation/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,49 @@ 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 mixed $endDate Excel date serial value (float), PHP date timestamp (integer),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type should enumerate all possibitilies here: \DateTime|int|float|string

* PHP DateTime object, or a standard date string
* @param mixed $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);

if (is_string($startDate = self::getDateValue($startDate))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid assignment within condition and do it beforehand instead

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

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 @@ -76,6 +76,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 @@ -436,6 +436,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
89 changes: 89 additions & 0 deletions tests/data/Calculation/DateTime/DAYS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests cases miss a few cases for different input type such as DateTime, int and float

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',
],
];