/
Range.php
204 lines (167 loc) · 4.11 KB
/
Range.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace duncan3dc\Dates;
/**
* A representation of a range of dates.
*/
class Range
{
/**
* @var DateTime $start The start date of this range.
*/
protected $start;
/**
* @var DateTime $end The end date of this range.
*/
protected $end;
/**
* Create a new range from 2 dates.
*
* @param DateTime $start The start date of the range
* @param DateTime $end The end date of the range
*/
public function __construct(DateTime $start, DateTime $end)
{
$this->start = $start;
$this->end = $end;
if ($this->end->timestamp() < $this->start->timestamp()) {
throw new \InvalidArgumentException("Invalid range, the start date must be before the end date");
}
}
/**
* Get the start date of this range.
*
* @return DateTime
*/
public function getStart()
{
return $this->start;
}
/**
* Get the end date of this range.
*
* @return DateTime
*/
public function getEnd()
{
return $this->end;
}
/**
* Get an iterator for the days covered by this date range.
*
* @return Iterator\Days
*/
public function days()
{
return new Iterator\Days($this->start, $this->end);
}
/**
* Get an iterator for the months covered by this date range.
*
* @return Iterator\Months
*/
public function months()
{
return new Iterator\Months($this->start, $this->end);
}
/**
* Get an iterator for the years covered by this date range.
*
* @return Iterator\Years
*/
public function years()
{
return new Iterator\Years($this->start, $this->end);
}
/**
* Get an iterator for the hours covered by this date range.
*
* @return Iterator\Hours
*/
public function hours()
{
return new Iterator\Hours($this->start, $this->end);
}
/**
* Get an iterator for the minutes covered by this date range.
*
* @return Iterator\Minutes
*/
public function minutes()
{
return new Iterator\Minutes($this->start, $this->end);
}
/**
* Get an iterator for the seconds covered by this date range.
*
* @return Iterator\Seconds
*/
public function seconds()
{
return new Iterator\Seconds($this->start, $this->end);
}
/**
* Get the range represented in a human readable format.
*
* @return string
*/
public function asString()
{
$plural = function ($number, $word) {
$number = (int) $number;
if ($number !== 1) {
$word .= "s";
}
return "{$number} {$word}";
};
$seconds = $this->end->timestamp() - $this->start->timestamp();
if ($seconds < 60) {
return $plural($seconds, "second");
}
$minutes = floor($seconds / 60);
$seconds %= 60;
if ($minutes < 60) {
if ($seconds >= 30) {
++$minutes;
}
return $plural($minutes, "minute");
}
$hours = floor($minutes / 60);
$minutes %= 60;
if ($hours < 24) {
if ($minutes >= 30) {
$hours++;
}
return $plural($hours, "hour");
}
$days = floor($hours / 24);
$hours %= 24;
if ($days < 7) {
if ($hours >= 12) {
++$days;
}
return $plural($days, "day");
}
$weeks = floor($days / 7);
$days %= 7;
if ($weeks < 4) {
if ($days > 3) {
++$weeks;
}
return $plural($weeks, "week");
}
$months = floor($weeks / 4);
$weeks %= 4;
if ($months < 12) {
if ($weeks > 1.9) {
++$months;
}
return $plural($months, "month");
}
$years = floor($months / 12);
$months %= 12;
if ($months > 6) {
++$years;
}
return $plural($years, "year");
}
}