Passed
Push — master ( b3452a...0c5808 )
by
unknown
18:22 queued 08:16
created

Days::between()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 9.1111
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
4
5
use DateTimeInterface;
6
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
7
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
10
11
class Days
12
{
13
    use ArrayEnabled;
14
15
    /**
16
     * DAYS.
17
     *
18
     * Returns the number of days between two dates
19
     *
20
     * Excel Function:
21
     *        DAYS(endDate, startDate)
22
     *
23
     * @param array|DateTimeInterface|float|int|string $endDate Excel date serial value (float),
24
     *           PHP date timestamp (integer), PHP DateTime object, or a standard date string
25
     *                         Or can be an array of date values
26
     * @param array|DateTimeInterface|float|int|string $startDate Excel date serial value (float),
27
     *           PHP date timestamp (integer), PHP DateTime object, or a standard date string
28
     *                         Or can be an array of date values
29
     *
30
     * @return array|int|string Number of days between start date and end date or an error
31
     *         If an array of values is passed for the $startDate or $endDays,arguments, then the returned result
32
     *            will also be an array with matching dimensions
33
     */
34 78
    public static function between(array|DateTimeInterface|float|int|string $endDate, array|DateTimeInterface|float|int|string $startDate): array|int|string
35
    {
36 78
        if (is_array($endDate) || is_array($startDate)) {
37 24
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $endDate, $startDate);
38
        }
39
40
        try {
41 78
            $startDate = Helpers::getDateValue($startDate);
42 75
            $endDate = Helpers::getDateValue($endDate);
43 6
        } catch (Exception $e) {
44 6
            return $e->getMessage();
45
        }
46
47
        // Execute function
48 72
        $PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
49 72
        $PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
50
51 72
        $days = ExcelError::VALUE();
52 72
        $diff = $PHPStartDateObject->diff($PHPEndDateObject);
53 72
        if (!is_bool($diff->days)) {
54 72
            $days = $diff->days;
55 72
            if ($diff->invert) {
56 20
                $days = -$days;
57
            }
58
        }
59
60 72
        return $days;
61
    }
62
}
63