DifferenceTrait.php
9.33 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use Cake\Chronos\ChronosInterval;
use Cake\Chronos\DifferenceFormatter;
use DatePeriod;
use DateTimeImmutable;
use DateTimeInterface;
/**
* Provides methods for getting differences between datetime objects.
*
* Expects that the implementing class implements:
*
* - static::now()
* - static::instance()
* - copy()
*/
trait DifferenceTrait
{
/**
* Instance of the diff formatting object.
*
* @var \Cake\Chronos\DifferenceFormatter
*/
protected static $diffFormatter;
/**
* Get the difference in years
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInYears(ChronosInterface $dt = null, $abs = true)
{
$dt = $dt === null ? static::now($this->tz) : $dt;
return (int)$this->diff($dt, $abs)->format('%r%y');
}
/**
* Get the difference in months
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMonths(ChronosInterface $dt = null, $abs = true)
{
$dt = $dt === null ? static::now($this->tz) : $dt;
return $this->diffInYears($dt, $abs) * ChronosInterface::MONTHS_PER_YEAR + (int)$this->diff($dt, $abs)->format('%r%m');
}
/**
* Get the difference in weeks
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeeks(ChronosInterface $dt = null, $abs = true)
{
return (int)($this->diffInDays($dt, $abs) / ChronosInterface::DAYS_PER_WEEK);
}
/**
* Get the difference in days
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDays(ChronosInterface $dt = null, $abs = true)
{
$dt = $dt === null ? static::now($this->tz) : $dt;
return (int)$this->diff($dt, $abs)->format('%r%a');
}
/**
* Get the difference in days using a filter callable
*
* @param callable $callback The callback to use for filtering.
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDaysFiltered(callable $callback, ChronosInterface $dt = null, $abs = true)
{
return $this->diffFiltered(ChronosInterval::day(), $callback, $dt, $abs);
}
/**
* Get the difference in hours using a filter callable
*
* @param callable $callback The callback to use for filtering.
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHoursFiltered(callable $callback, ChronosInterface $dt = null, $abs = true)
{
return $this->diffFiltered(ChronosInterval::hour(), $callback, $dt, $abs);
}
/**
* Get the difference by the given interval using a filter callable
*
* @param ChronosInterval $ci An interval to traverse by
* @param callable $callback The callback to use for filtering.
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffFiltered(ChronosInterval $ci, callable $callback, ChronosInterface $dt = null, $abs = true)
{
$start = $this;
$end = $dt === null ? static::now($this->tz) : $dt;
$inverse = false;
if (defined('HHVM_VERSION')) {
$start = new DateTimeImmutable($this->toIso8601String());
$end = new DateTimeImmutable($end->toIso8601String());
}
if ($end < $start) {
$start = $end;
$end = $this;
$inverse = true;
}
$period = new DatePeriod($start, $ci, $end);
$vals = array_filter(iterator_to_array($period), function (DateTimeInterface $date) use ($callback) {
return $callback(static::instance($date));
});
$diff = count($vals);
return $inverse && !$abs ? -$diff : $diff;
}
/**
* Get the difference in weekdays
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekdays(ChronosInterface $dt = null, $abs = true)
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekday();
}, $dt, $abs);
}
/**
* Get the difference in weekend days using a filter
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekendDays(ChronosInterface $dt = null, $abs = true)
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekend();
}, $dt, $abs);
}
/**
* Get the difference in hours
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHours(ChronosInterface $dt = null, $abs = true)
{
return (int)($this->diffInSeconds($dt, $abs) / ChronosInterface::SECONDS_PER_MINUTE / ChronosInterface::MINUTES_PER_HOUR);
}
/**
* Get the difference in minutes
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMinutes(ChronosInterface $dt = null, $abs = true)
{
return (int)($this->diffInSeconds($dt, $abs) / ChronosInterface::SECONDS_PER_MINUTE);
}
/**
* Get the difference in seconds
*
* @param ChronosInterface|null $dt The instance to difference from.
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInSeconds(ChronosInterface $dt = null, $abs = true)
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
$value = $dt->getTimestamp() - $this->getTimestamp();
return $abs ? abs($value) : $value;
}
/**
* The number of seconds since midnight.
*
* @return int
*/
public function secondsSinceMidnight()
{
return $this->diffInSeconds($this->copy()->startOfDay());
}
/**
* The number of seconds until 23:23:59.
*
* @return int
*/
public function secondsUntilEndOfDay()
{
return $this->diffInSeconds($this->copy()->endOfDay());
}
/**
* Convenience method for getting the remaining time from a given time.
*
* @param \DateTime|\DateTimeImmutable $datetime The date to get the remaining time from.
* @return \DateInterval|bool The DateInterval object representing the difference between the two dates or FALSE on failure.
*/
public static function fromNow($datetime)
{
$timeNow = new static();
return $timeNow->diff($datetime);
}
/**
* Get the difference in a human readable format.
*
* When comparing a value in the past to default now:
* 1 hour ago
* 5 months ago
*
* When comparing a value in the future to default now:
* 1 hour from now
* 5 months from now
*
* When comparing a value in the past to another value:
* 1 hour before
* 5 months before
*
* When comparing a value in the future to another value:
* 1 hour after
* 5 months after
*
* @param \Cake\Chronos\ChronosInterface|null $other The datetime to compare with.
* @param bool $absolute removes time difference modifiers ago, after, etc
* @return string
*/
public function diffForhumans(ChronosInterface $other = null, $absolute = false)
{
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
}
/**
* Get the difference formatter instance or overwrite the current one.
*
* @param \Cake\Chronos\DifferenceFormatter|null $formatter The formatter instance when setting.
* @return \Cake\Chronos\DifferenceFormatter The formatter instance.
*/
public static function diffFormatter($formatter = null)
{
if ($formatter === null) {
if (static::$diffFormatter === null) {
static::$diffFormatter = new DifferenceFormatter();
}
return static::$diffFormatter;
}
return static::$diffFormatter = $translator;
}
}