Skip to content

Commit 2c981e4

Browse files
Biser AntonovPowerKiKi
Biser Antonov
authored andcommitted
1 parent a1e8c84 commit 2c981e4

File tree

6 files changed

+172
-1
lines changed

6 files changed

+172
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add the DAYS() function - [#594](https://github.com/PHPOffice/PhpSpreadsheet/pull/594)
13+
1014
### Fixed
1115

1216
- Sheet title can contain exclamation mark - [#325](https://github.com/PHPOffice/PhpSpreadsheet/issues/325)

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,11 @@ class Calculation
650650
'functionCall' => [DateTime::class, 'DAYOFMONTH'],
651651
'argumentCount' => '1',
652652
],
653+
'DAYS' => [
654+
'category' => Category::CATEGORY_DATE_AND_TIME,
655+
'functionCall' => [DateTime::class, 'DAYS'],
656+
'argumentCount' => '2',
657+
],
653658
'DAYS360' => [
654659
'category' => Category::CATEGORY_DATE_AND_TIME,
655660
'functionCall' => [DateTime::class, 'DAYS360'],

src/PhpSpreadsheet/Calculation/DateTime.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function getDateValue($dateValue)
7070
(Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC)) {
7171
return Functions::VALUE();
7272
}
73-
if ((is_object($dateValue)) && ($dateValue instanceof \DateTimeImmutable)) {
73+
if ((is_object($dateValue)) && ($dateValue instanceof \DateTimeInterface)) {
7474
$dateValue = Date::PHPToExcel($dateValue);
7575
} else {
7676
$saveReturnDateType = Functions::getReturnDateType();
@@ -764,6 +764,52 @@ public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D')
764764
return $retVal;
765765
}
766766

767+
/**
768+
* DAYS.
769+
*
770+
* Returns the number of days between two dates
771+
*
772+
* Excel Function:
773+
* DAYS(endDate, startDate)
774+
*
775+
* @category Date/Time Functions
776+
*
777+
* @param \DateTimeImmutable|float|int|string $endDate Excel date serial value (float),
778+
* PHP date timestamp (integer), PHP DateTime object, or a standard date string
779+
* @param \DateTimeImmutable|float|int|string $startDate Excel date serial value (float),
780+
* PHP date timestamp (integer), PHP DateTime object, or a standard date string
781+
*
782+
* @return int|string Number of days between start date and end date or an error
783+
*/
784+
public static function DAYS($endDate = 0, $startDate = 0)
785+
{
786+
$startDate = Functions::flattenSingleValue($startDate);
787+
$endDate = Functions::flattenSingleValue($endDate);
788+
789+
$startDate = self::getDateValue($startDate);
790+
if (is_string($startDate)) {
791+
return Functions::VALUE();
792+
}
793+
794+
$endDate = self::getDateValue($endDate);
795+
if (is_string($endDate)) {
796+
return Functions::VALUE();
797+
}
798+
799+
// Execute function
800+
$PHPStartDateObject = Date::excelToDateTimeObject($startDate);
801+
$PHPEndDateObject = Date::excelToDateTimeObject($endDate);
802+
803+
$diff = $PHPStartDateObject->diff($PHPEndDateObject);
804+
$days = $diff->days;
805+
806+
if ($diff->invert) {
807+
$days = -$days;
808+
}
809+
810+
return $days;
811+
}
812+
767813
/**
768814
* DAYS360.
769815
*

src/PhpSpreadsheet/Calculation/functionlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ DATEDIF
8888
DATEVALUE
8989
DAVERAGE
9090
DAY
91+
DAYS
9192
DAYS360
9293
DB
9394
DCOUNT

tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,22 @@ public function providerDATEDIF()
453453
return require 'data/Calculation/DateTime/DATEDIF.php';
454454
}
455455

456+
/**
457+
* @dataProvider providerDAYS
458+
*
459+
* @param mixed $expectedResult
460+
*/
461+
public function testDAYS($expectedResult, ...$args)
462+
{
463+
$result = DateTime::DAYS(...$args);
464+
self::assertEquals($expectedResult, $result, null, 1E-8);
465+
}
466+
467+
public function providerDAYS()
468+
{
469+
return require 'data/Calculation/DateTime/DAYS.php';
470+
}
471+
456472
/**
457473
* @dataProvider providerDAYS360
458474
*
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
return [
4+
[
5+
'#VALUE!',
6+
'2007-1-10',
7+
'ABC',
8+
],
9+
[
10+
'#VALUE!',
11+
'DEF',
12+
'2007-1-1',
13+
],
14+
[
15+
9,
16+
'2007-1-10',
17+
'2007-1-1',
18+
],
19+
[
20+
364,
21+
'2007-12-31',
22+
'2007-1-1',
23+
],
24+
[
25+
547,
26+
'2008-7-1',
27+
'2007-1-1',
28+
],
29+
[
30+
30,
31+
'2007-1-31',
32+
'2007-1-1',
33+
],
34+
[
35+
31,
36+
'2007-2-1',
37+
'2007-1-1',
38+
],
39+
[
40+
58,
41+
'2007-2-28',
42+
'2007-1-1',
43+
],
44+
[
45+
1,
46+
'2007-2-1',
47+
'2007-1-31',
48+
],
49+
[
50+
29,
51+
'2007-3-1',
52+
'2007-1-31',
53+
],
54+
[
55+
59,
56+
'2007-3-31',
57+
'2007-1-31',
58+
],
59+
[
60+
244,
61+
'2008-9-1',
62+
'2008-1-1',
63+
],
64+
[
65+
425,
66+
'2008-4-1',
67+
'2007-2-1',
68+
],
69+
[
70+
17358,
71+
'2008-6-28',
72+
'1960-12-19',
73+
],
74+
[
75+
9335,
76+
'2008-6-28',
77+
'1982-12-7',
78+
],
79+
[
80+
32,
81+
'2000-3-31',
82+
'2000-2-28',
83+
],
84+
[
85+
31,
86+
'2000-3-31',
87+
'2000-2-29',
88+
],
89+
[
90+
31,
91+
new \DateTime('2000-3-31'),
92+
new \DateTimeImmutable('2000-2-29'),
93+
],
94+
[
95+
31,
96+
36616,
97+
36585,
98+
],
99+
];

0 commit comments

Comments
 (0)