DateTimeUtil   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 118
c 0
b 0
f 0
wmc 13
lcom 0
cbo 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isWeekendDay() 0 4 1
A isWeekDay() 0 4 1
A isSameDay() 0 5 3
A isDaylightSavings() 0 4 1
A isInRange() 0 14 3
A asAgoString() 0 29 4
1
<?php
2
3
namespace SubjectivePHP\DateTime\Utilities;
4
5
use DateTimeInterface;
6
use SubjectivePHP\DateTime\Constants\DayOfWeekInterface;
7
8
/**
9
 * Utility class for DateTimeInterface objects.
10
 */
11
abstract class DateTimeUtil implements DayOfWeekInterface
12
{
13
    /**
14
     * Returns true if the given date time is a Saturday or Sunday.
15
     *
16
     * @param DateTimeInterface $dateTime The date/time object.
17
     *
18
     * @return bool
19
     */
20
    final public static function isWeekendDay(DateTimeInterface $dateTime) : bool
21
    {
22
        return (int)$dateTime->format('N') > (int)self::ISO_FRIDAY;
23
    }
24
25
    /**
26
     * Returns true if the given date time is not a Saturday or Sunday.
27
     *
28
     * @param DateTimeInterface $dateTime The date/time object.
29
     *
30
     * @return bool
31
     */
32
    final public static function isWeekDay(DateTimeInterface $dateTime) : bool
33
    {
34
        return (int)$dateTime->format('N') < (int)self::ISO_SATURDAY;
35
    }
36
37
    /**
38
     * Returns true if the given dates occur on the same year, month and day.
39
     *
40
     * @param DateTimeInterface $thisDate A date to compare.
41
     * @param DateTimeInterface $thatDate A date to compare.
42
     *
43
     * @return bool
44
     */
45
    final public static function isSameDay(DateTimeInterface $thisDate, DateTimeInterface $thatDate) : bool
46
    {
47
        $interval = $thisDate->diff($thatDate);
48
        return !$interval->y && !$interval->m && !$interval->d;
49
    }
50
51
    /**
52
     * Indicates whether the given instance of DateTime is within the daylight saving time range for the current time
53
     * zone.
54
     *
55
     * @param DateTimeInterface $dateTime The date/time object.
56
     *
57
     * @return bool
58
     */
59
    final public static function isDaylightSavings(DateTimeInterface $dateTime) : bool
60
    {
61
        return (bool)$dateTime->format('I');
62
    }
63
64
    /**
65
     * Returns true if the given date is between the provided date range.
66
     *
67
     * @param DateTimeInterface $subjectDate The date/time object being checked.
68
     * @param DateTimeInterface $startDate   The start date/time object.
69
     * @param DateTimeInterface $endDate     The end date/time object.
70
     *
71
     * @return bool
72
     *
73
     * @throws \DomainException Thrown when an invalid date range is provided.
74
     */
75
    final public static function isInRange(
76
        DateTimeInterface $subjectDate,
77
        DateTimeInterface $startDate,
78
        DateTimeInterface $endDate
79
    ) : bool {
80
        $subjectTimestamp = $subjectDate->getTimestamp();
81
        $startDateTimestamp = $startDate->getTimestamp();
82
        $endDateTimestamp = $endDate->getTimestamp();
83
        if ($startDateTimestamp > $endDateTimestamp) {
84
            throw new \DomainException('Invalid date range provided.');
85
        }
86
87
        return ($subjectTimestamp >= $startDateTimestamp && $subjectTimestamp <= $endDateTimestamp);
88
    }
89
90
    /**
91
     * Returns the given DateTime instance as a "time ago" string.
92
     *
93
     * @param DateTimeInterface $dateTime The DateTime object to present as an ago string.
94
     *
95
     * @return string
96
     *
97
     * @throws \DomainException Thrown if the given $dateTime is in the future.
98
     */
99
    final public static function asAgoString(DateTimeInterface $dateTime) : string
100
    {
101
        $numHours = round((time() - $dateTime->getTimestamp())/3600, 2);
102
        if ($numHours < 0) {
103
            throw new \DomainException('Cannot create "time ago" string for a date in the future.');
104
        }
105
106
        $formulas = [
107
            '.025' => ['just now', 1],
108
            '.5' => ['%d minutes ago', 60],
109
            '1.5' => ['about an hour ago', 1],
110
            '24' => ['about %d hours ago', 1],
111
            '48' => ['yesterday', 1],
112
            '168' => ['about %d days ago', 1/24],
113
            '252' => ['last week', 1],
114
            '672' => ['about %d weeks ago', 1/168],
115
            '1080' => ['last month', 1],
116
            '8760' => ['about %d months ago', 1/672],
117
            '13140' => ['last year', 1],
118
        ];
119
120
        foreach ($formulas as $maxHours => list($format, $multiplier)) {
121
            if ($numHours < $maxHours) {
122
                return sprintf($format, round($numHours * $multiplier));
123
            }
124
        }
125
126
        return sprintf('about %s years ago', round($numHours/8064));
127
    }
128
}
129