-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
* 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))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ DATEDIF | |
DATEVALUE | ||
DAVERAGE | ||
DAY | ||
DAYS | ||
DAYS360 | ||
DB | ||
DCOUNT | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
], | ||
]; |
There was a problem hiding this comment.
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