3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\I18n;
20
* Class TimeDifference
22
* @see \CodeIgniter\I18n\TimeDifferenceTest
27
* The timestamp of the "current" time.
31
protected $currentTime;
34
* The timestamp to compare the current time to.
59
protected $months = 0;
87
protected $minutes = 0;
94
protected $seconds = 0;
97
* Difference in seconds.
101
protected $difference;
104
* Note: both parameters are required to be in the same timezone. No timezone
105
* shifting is done internally.
107
public function __construct(DateTime $currentTime, DateTime $testTime)
109
$this->difference = $currentTime->getTimestamp() - $testTime->getTimestamp();
111
$current = IntlCalendar::fromDateTime($currentTime);
112
$time = IntlCalendar::fromDateTime($testTime)->getTime();
114
$this->currentTime = $current;
115
$this->testTime = $time;
119
* Returns the number of years of difference between the two.
123
public function getYears(bool $raw = false)
126
return $this->difference / YEAR;
129
$time = clone $this->currentTime;
131
return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_YEAR);
135
* Returns the number of months difference between the two dates.
139
public function getMonths(bool $raw = false)
142
return $this->difference / MONTH;
145
$time = clone $this->currentTime;
147
return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_MONTH);
151
* Returns the number of weeks difference between the two dates.
155
public function getWeeks(bool $raw = false)
158
return $this->difference / WEEK;
161
$time = clone $this->currentTime;
163
return (int) ($time->fieldDifference($this->testTime, IntlCalendar::FIELD_DAY_OF_YEAR) / 7);
167
* Returns the number of days difference between the two dates.
171
public function getDays(bool $raw = false)
174
return $this->difference / DAY;
177
$time = clone $this->currentTime;
179
return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_DAY_OF_YEAR);
183
* Returns the number of hours difference between the two dates.
187
public function getHours(bool $raw = false)
190
return $this->difference / HOUR;
193
$time = clone $this->currentTime;
195
return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_HOUR_OF_DAY);
199
* Returns the number of minutes difference between the two dates.
203
public function getMinutes(bool $raw = false)
206
return $this->difference / MINUTE;
209
$time = clone $this->currentTime;
211
return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_MINUTE);
215
* Returns the number of seconds difference between the two dates.
219
public function getSeconds(bool $raw = false)
222
return $this->difference;
225
$time = clone $this->currentTime;
227
return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_SECOND);
231
* Convert the time to human readable format
233
public function humanize(?string $locale = null): string
235
$current = clone $this->currentTime;
237
$years = $current->fieldDifference($this->testTime, IntlCalendar::FIELD_YEAR);
238
$months = $current->fieldDifference($this->testTime, IntlCalendar::FIELD_MONTH);
239
$days = $current->fieldDifference($this->testTime, IntlCalendar::FIELD_DAY_OF_YEAR);
240
$hours = $current->fieldDifference($this->testTime, IntlCalendar::FIELD_HOUR_OF_DAY);
241
$minutes = $current->fieldDifference($this->testTime, IntlCalendar::FIELD_MINUTE);
246
$phrase = lang('Time.years', [abs($years)], $locale);
247
$before = $years < 0;
248
} elseif ($months !== 0) {
249
$phrase = lang('Time.months', [abs($months)], $locale);
250
$before = $months < 0;
251
} elseif ($days !== 0 && (abs($days) >= 7)) {
252
$weeks = ceil($days / 7);
253
$phrase = lang('Time.weeks', [abs($weeks)], $locale);
255
} elseif ($days !== 0) {
256
$phrase = lang('Time.days', [abs($days)], $locale);
258
} elseif ($hours !== 0) {
259
$phrase = lang('Time.hours', [abs($hours)], $locale);
260
$before = $hours < 0;
261
} elseif ($minutes !== 0) {
262
$phrase = lang('Time.minutes', [abs($minutes)], $locale);
263
$before = $minutes < 0;
265
return lang('Time.now', [], $locale);
269
? lang('Time.ago', [$phrase], $locale)
270
: lang('Time.inFuture', [$phrase], $locale);
274
* Allow property-like access to our calculated values.
276
* @param string $name
278
* @return float|int|null
280
public function __get($name)
282
$name = ucfirst(strtolower($name));
283
$method = "get{$name}";
285
if (method_exists($this, $method)) {
286
return $this->{$method}();
293
* Allow property-like checking for our calculated values.
295
* @param string $name
299
public function __isset($name)
301
$name = ucfirst(strtolower($name));
302
$method = "get{$name}";
304
return method_exists($this, $method);