Code Duplication    Length = 115-120 lines in 2 locations

src/DateTime/Utilities/DateTimeUtil.php 1 location

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

src/Util/DateTime.php 1 location

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