|
1
|
|
|
<?php |
|
2
|
|
|
namespace SubjectivePHP\Util; |
|
3
|
|
|
|
|
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
|
|
|
|