24 public static function interval($startDate, $endDate, $unit =
'D')
29 $difference = self::initialDiff($startDate, $endDate);
32 return $e->getMessage();
36 $PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
37 $startDays = (int) $PHPStartDateObject->format(
'j');
38 $startMonths = (int) $PHPStartDateObject->format(
'n');
39 $startYears = (int) $PHPStartDateObject->format(
'Y');
41 $PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
42 $endDays = (int) $PHPEndDateObject->format(
'j');
43 $endMonths = (int) $PHPEndDateObject->format(
'n');
44 $endYears = (int) $PHPEndDateObject->format(
'Y');
46 $PHPDiffDateObject = $PHPEndDateObject->diff($PHPStartDateObject);
49 $retVal = self::replaceRetValue($retVal, $unit,
'D') ?? self::datedifD($difference);
50 $retVal = self::replaceRetValue($retVal, $unit,
'M') ?? self::datedifM($PHPDiffDateObject);
51 $retVal = self::replaceRetValue($retVal, $unit,
'MD') ?? self::datedifMD($startDays, $endDays, $PHPEndDateObject, $PHPDiffDateObject);
52 $retVal = self::replaceRetValue($retVal, $unit,
'Y') ?? self::datedifY($PHPDiffDateObject);
53 $retVal = self::replaceRetValue($retVal, $unit,
'YD') ?? self::datedifYD($difference, $startYears, $endYears, $PHPStartDateObject, $PHPEndDateObject);
54 $retVal = self::replaceRetValue($retVal, $unit,
'YM') ?? self::datedifYM($PHPDiffDateObject);
59 private static function initialDiff(
float $startDate,
float $endDate): float
62 if ($startDate > $endDate) {
66 return $endDate - $startDate;
78 if ($retVal !==
false || $unit !== $compare) {
85 private static function datedifD(
float $difference): int
87 return (
int) $difference;
92 return 12 * (int) $PHPDiffDateObject->format(
'%y') + (int) $PHPDiffDateObject->format(
'%m');
97 if ($endDays < $startDays) {
99 $PHPEndDateObject->modify(
'-' . $endDays .
' days');
100 $adjustDays = (int) $PHPEndDateObject->format(
'j');
101 $retVal += ($adjustDays - $startDays);
103 $retVal = (int) $PHPDiffDateObject->format(
'%d');
111 return (
int) $PHPDiffDateObject->format(
'%y');
114 private static function datedifYD(
float $difference,
int $startYears,
int $endYears,
DateTime $PHPStartDateObject,
DateTime $PHPEndDateObject): int
116 $retVal = (int) $difference;
117 if ($endYears > $startYears) {
118 $isLeapStartYear = $PHPStartDateObject->format(
'L');
119 $wasLeapEndYear = $PHPEndDateObject->format(
'L');
122 while ($PHPEndDateObject >= $PHPStartDateObject) {
123 $PHPEndDateObject->modify(
'-1 year');
124 $endYears = $PHPEndDateObject->format(
'Y');
126 $PHPEndDateObject->modify(
'+1 year');
129 $retVal = $PHPEndDateObject->diff($PHPStartDateObject)->days;
132 $isLeapEndYear = $PHPEndDateObject->format(
'L');
133 $limit =
new DateTime($PHPEndDateObject->format(
'Y-02-29'));
134 if (!$isLeapStartYear && !$wasLeapEndYear && $isLeapEndYear && $PHPEndDateObject >= $limit) {
139 return (
int) $retVal;
144 return (
int) $PHPDiffDateObject->format(
'%m');
static datedifY(DateInterval $PHPDiffDateObject)
static datedifD(float $difference)
static interval($startDate, $endDate, $unit='D')
DATEDIF.
static datedifYD(float $difference, int $startYears, int $endYears, DateTime $PHPStartDateObject, DateTime $PHPEndDateObject)
static replaceRetValue($retVal, string $unit, string $compare)
Decide whether it's time to set retVal.
static datedifYM(DateInterval $PHPDiffDateObject)
static datedifM(DateInterval $PHPDiffDateObject)
static datedifMD(int $startDays, int $endDays, DateTime $PHPEndDateObject, DateInterval $PHPDiffDateObject)
static getDateValue($dateValue, bool $allowBool=true)
getDateValue.
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
static initialDiff(float $startDate, float $endDate)