Biser Antonov 2018-07-11 15:50:09 +03:00 committed by Adrien Crivelli
parent a1e8c843b7
commit 2c981e47a1
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
6 changed files with 172 additions and 1 deletions

View File

@ -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)

View File

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

View File

@ -70,7 +70,7 @@ class DateTime
(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();
@ -764,6 +764,52 @@ class DateTime
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.
*

View File

@ -88,6 +88,7 @@ DATEDIF
DATEVALUE
DAVERAGE
DAY
DAYS
DAYS360
DB
DCOUNT

View File

@ -453,6 +453,22 @@ class DateTimeTest extends TestCase
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
*

View File

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