Completed
Pull Request — master (#40)
by Chad
04:02
created

DateTimeUtil::isWeekendDay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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