ci4

Форк
0
/
TimeDifference.php 
306 строк · 6.9 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\I18n;
15

16
use DateTime;
17
use IntlCalendar;
18

19
/**
20
 * Class TimeDifference
21
 *
22
 * @see \CodeIgniter\I18n\TimeDifferenceTest
23
 */
24
class TimeDifference
25
{
26
    /**
27
     * The timestamp of the "current" time.
28
     *
29
     * @var IntlCalendar
30
     */
31
    protected $currentTime;
32

33
    /**
34
     * The timestamp to compare the current time to.
35
     *
36
     * @var float
37
     */
38
    protected $testTime;
39

40
    /**
41
     * Eras.
42
     *
43
     * @var float
44
     */
45
    protected $eras = 0;
46

47
    /**
48
     * Years.
49
     *
50
     * @var float
51
     */
52
    protected $years = 0;
53

54
    /**
55
     * Months.
56
     *
57
     * @var float
58
     */
59
    protected $months = 0;
60

61
    /**
62
     * Weeks.
63
     *
64
     * @var int
65
     */
66
    protected $weeks = 0;
67

68
    /**
69
     * Days.
70
     *
71
     * @var int
72
     */
73
    protected $days = 0;
74

75
    /**
76
     * Hours.
77
     *
78
     * @var int
79
     */
80
    protected $hours = 0;
81

82
    /**
83
     * Minutes.
84
     *
85
     * @var int
86
     */
87
    protected $minutes = 0;
88

89
    /**
90
     * Seconds.
91
     *
92
     * @var int
93
     */
94
    protected $seconds = 0;
95

96
    /**
97
     * Difference in seconds.
98
     *
99
     * @var int
100
     */
101
    protected $difference;
102

103
    /**
104
     * Note: both parameters are required to be in the same timezone. No timezone
105
     * shifting is done internally.
106
     */
107
    public function __construct(DateTime $currentTime, DateTime $testTime)
108
    {
109
        $this->difference = $currentTime->getTimestamp() - $testTime->getTimestamp();
110

111
        $current = IntlCalendar::fromDateTime($currentTime);
112
        $time    = IntlCalendar::fromDateTime($testTime)->getTime();
113

114
        $this->currentTime = $current;
115
        $this->testTime    = $time;
116
    }
117

118
    /**
119
     * Returns the number of years of difference between the two.
120
     *
121
     * @return float|int
122
     */
123
    public function getYears(bool $raw = false)
124
    {
125
        if ($raw) {
126
            return $this->difference / YEAR;
127
        }
128

129
        $time = clone $this->currentTime;
130

131
        return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_YEAR);
132
    }
133

134
    /**
135
     * Returns the number of months difference between the two dates.
136
     *
137
     * @return float|int
138
     */
139
    public function getMonths(bool $raw = false)
140
    {
141
        if ($raw) {
142
            return $this->difference / MONTH;
143
        }
144

145
        $time = clone $this->currentTime;
146

147
        return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_MONTH);
148
    }
149

150
    /**
151
     * Returns the number of weeks difference between the two dates.
152
     *
153
     * @return float|int
154
     */
155
    public function getWeeks(bool $raw = false)
156
    {
157
        if ($raw) {
158
            return $this->difference / WEEK;
159
        }
160

161
        $time = clone $this->currentTime;
162

163
        return (int) ($time->fieldDifference($this->testTime, IntlCalendar::FIELD_DAY_OF_YEAR) / 7);
164
    }
165

166
    /**
167
     * Returns the number of days difference between the two dates.
168
     *
169
     * @return float|int
170
     */
171
    public function getDays(bool $raw = false)
172
    {
173
        if ($raw) {
174
            return $this->difference / DAY;
175
        }
176

177
        $time = clone $this->currentTime;
178

179
        return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_DAY_OF_YEAR);
180
    }
181

182
    /**
183
     * Returns the number of hours difference between the two dates.
184
     *
185
     * @return float|int
186
     */
187
    public function getHours(bool $raw = false)
188
    {
189
        if ($raw) {
190
            return $this->difference / HOUR;
191
        }
192

193
        $time = clone $this->currentTime;
194

195
        return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_HOUR_OF_DAY);
196
    }
197

198
    /**
199
     * Returns the number of minutes difference between the two dates.
200
     *
201
     * @return float|int
202
     */
203
    public function getMinutes(bool $raw = false)
204
    {
205
        if ($raw) {
206
            return $this->difference / MINUTE;
207
        }
208

209
        $time = clone $this->currentTime;
210

211
        return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_MINUTE);
212
    }
213

214
    /**
215
     * Returns the number of seconds difference between the two dates.
216
     *
217
     * @return int
218
     */
219
    public function getSeconds(bool $raw = false)
220
    {
221
        if ($raw) {
222
            return $this->difference;
223
        }
224

225
        $time = clone $this->currentTime;
226

227
        return $time->fieldDifference($this->testTime, IntlCalendar::FIELD_SECOND);
228
    }
229

230
    /**
231
     * Convert the time to human readable format
232
     */
233
    public function humanize(?string $locale = null): string
234
    {
235
        $current = clone $this->currentTime;
236

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);
242

243
        $phrase = null;
244

245
        if ($years !== 0) {
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);
254
            $before = $days < 0;
255
        } elseif ($days !== 0) {
256
            $phrase = lang('Time.days', [abs($days)], $locale);
257
            $before = $days < 0;
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;
264
        } else {
265
            return lang('Time.now', [], $locale);
266
        }
267

268
        return $before
269
            ? lang('Time.ago', [$phrase], $locale)
270
            : lang('Time.inFuture', [$phrase], $locale);
271
    }
272

273
    /**
274
     * Allow property-like access to our calculated values.
275
     *
276
     * @param string $name
277
     *
278
     * @return float|int|null
279
     */
280
    public function __get($name)
281
    {
282
        $name   = ucfirst(strtolower($name));
283
        $method = "get{$name}";
284

285
        if (method_exists($this, $method)) {
286
            return $this->{$method}();
287
        }
288

289
        return null;
290
    }
291

292
    /**
293
     * Allow property-like checking for our calculated values.
294
     *
295
     * @param string $name
296
     *
297
     * @return bool
298
     */
299
    public function __isset($name)
300
    {
301
        $name   = ucfirst(strtolower($name));
302
        $method = "get{$name}";
303

304
        return method_exists($this, $method);
305
    }
306
}
307

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.