1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Lenevor Framework |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file license.md. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* https://lenevor.com/license |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @package Lenevor |
17
|
|
|
* @subpackage Base |
18
|
|
|
* @link https://lenevor.com |
19
|
|
|
* @copyright Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> |
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Syscodes\Support\Chronos\Traits; |
24
|
|
|
|
25
|
|
|
use Locale; |
26
|
|
|
use DateTime; |
27
|
|
|
use DateTimeZone; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Trait Factory. |
31
|
|
|
* |
32
|
|
|
* Static factories. |
33
|
|
|
* |
34
|
|
|
* @author Alexander Campo <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
trait Factory |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Constructor. The Date class instance. |
40
|
|
|
* |
41
|
|
|
* @param string|null $time |
42
|
|
|
* @param string|null $timezone |
43
|
|
|
* @param string|null $locale |
44
|
|
|
*/ |
45
|
|
|
public function __construct(string $time = null, $timezone = null, string $locale = null) |
46
|
|
|
{ |
47
|
|
|
$this->locale = ! empty($locale) ? $locale : Locale::getDefault(); |
|
|
|
|
48
|
|
|
|
49
|
|
|
if (is_null($time) && static::$testNow instanceof static) { |
50
|
|
|
if (empty($timezone)) { |
51
|
|
|
$timezone = static::$testNow->getTimezone(); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$time = static::$testNow->toDateTimeString(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$timezone = ! empty($timezone) ? $timezone : date_default_timezone_get(); |
58
|
|
|
$this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); |
|
|
|
|
59
|
|
|
|
60
|
|
|
if ( ! empty($time)) { |
61
|
|
|
if (is_string($time) && static::hasRelativeKeywords($time)) { |
62
|
|
|
$dateTime = new DateTime('now', $this->timezone); |
63
|
|
|
$dateTime->modify($time); |
64
|
|
|
|
65
|
|
|
$time = $dateTime->format('Y-m-d H:i:s'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return parent::__construct($time, $this->timezone); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a new Time instance with the timezone set. |
74
|
|
|
* |
75
|
|
|
* @param string|null $timezone |
76
|
|
|
* @param string|null $locale |
77
|
|
|
* |
78
|
|
|
* @return \Syscodes\Support\Chronos\Time |
79
|
|
|
*/ |
80
|
|
|
public static function now($timezone = null, string $locale = null) |
81
|
|
|
{ |
82
|
|
|
return new static(null, $timezone, $locale); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns a new Time instance while parsing a datetime string. |
87
|
|
|
* |
88
|
|
|
* @param string $time |
89
|
|
|
* @param string|null $timezone |
90
|
|
|
* @param string|null $locale |
91
|
|
|
* |
92
|
|
|
* @return \Syscodes\Support\Chronos\Time |
93
|
|
|
*/ |
94
|
|
|
public static function parse(string $time, $timezone = null, string $locale = null) |
95
|
|
|
{ |
96
|
|
|
return new static($time, $timezone, $locale); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return a new time with the time set to midnight. |
101
|
|
|
* |
102
|
|
|
* @param string|null $timezone |
103
|
|
|
* @param string|null $locale |
104
|
|
|
* |
105
|
|
|
* @return \Syscodes\Support\Chronos\Time |
106
|
|
|
*/ |
107
|
|
|
public static function today($timezone = null, string $locale = null) |
108
|
|
|
{ |
109
|
|
|
return static::parse(date('Y-m-d 00:00:00'), $timezone, $locale); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns an instance set to midnight yesterday morning. |
114
|
|
|
* |
115
|
|
|
* @param string|null $timezone |
116
|
|
|
* @param string|null $locale |
117
|
|
|
* |
118
|
|
|
* @return \Syscodes\Support\Chronos\Time |
119
|
|
|
*/ |
120
|
|
|
public static function yesterday($timezone = null, string $locale = null) |
121
|
|
|
{ |
122
|
|
|
return static::parse(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns an instance set to midnight tomorrow morning. |
127
|
|
|
* |
128
|
|
|
* @param string|null $timezone |
129
|
|
|
* @param string|null $locale |
130
|
|
|
* |
131
|
|
|
* @return \Syscodes\Support\Chronos\Time |
132
|
|
|
*/ |
133
|
|
|
public static function tomorrow($timezone = null, string $locale = null) |
134
|
|
|
{ |
135
|
|
|
return static::parse(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns a new instance based on the year, month and day. |
140
|
|
|
* If any of those three are left empty, will default to the current value. |
141
|
|
|
* |
142
|
|
|
* @param int|null $year |
143
|
|
|
* @param int|null $month |
144
|
|
|
* @param int|null $day |
145
|
|
|
* @param string|null $timezone |
146
|
|
|
* @param string|null $locale |
147
|
|
|
* |
148
|
|
|
* @return \Syscodes\Support\Chronos\Time |
149
|
|
|
*/ |
150
|
|
|
public static function createFromDate( |
151
|
|
|
int $year = null, |
152
|
|
|
int $month = null, |
153
|
|
|
int $day = null, |
154
|
|
|
$timezone = null, |
155
|
|
|
string $locale = null |
156
|
|
|
) { |
157
|
|
|
return static::create($year, $month, $day, null, null, null, $timezone, $locale); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns a new instance with the date set to today, and |
162
|
|
|
* the time set to the values passed in. |
163
|
|
|
* |
164
|
|
|
* @param int|null $hour |
165
|
|
|
* @param int|null $minutes |
166
|
|
|
* @param int|null $seconds |
167
|
|
|
* @param string|null $timezone |
168
|
|
|
* @param string|null $locale |
169
|
|
|
* |
170
|
|
|
* @return \Syscodes\Support\Chronos\Time |
171
|
|
|
*/ |
172
|
|
|
public static function createFromTime( |
173
|
|
|
int $hour = null, |
174
|
|
|
int $minutes = null, |
175
|
|
|
int $seconds = null, |
176
|
|
|
$timezone = null, |
177
|
|
|
string $locale = null |
178
|
|
|
) { |
179
|
|
|
return static::create(null, null, null, $hour, $minutes, $seconds, $timezone, $locale); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns a new instance with the date time values individually set. |
184
|
|
|
* |
185
|
|
|
* @param int|null $year |
186
|
|
|
* @param int|null $month |
187
|
|
|
* @param int|null $day |
188
|
|
|
* @param int|null $hour |
189
|
|
|
* @param int|null $minutes |
190
|
|
|
* @param int|null $seconds |
191
|
|
|
* @param string|null $timezone |
192
|
|
|
* @param string|null $locale |
193
|
|
|
* |
194
|
|
|
* @return \Syscodes\Support\Chronos\Time |
195
|
|
|
*/ |
196
|
|
|
public static function create( |
197
|
|
|
int $year = null, |
198
|
|
|
int $month = null, |
199
|
|
|
int $day = null, |
200
|
|
|
int $hour = null, |
201
|
|
|
int $minutes = null, |
202
|
|
|
int $seconds = null, |
203
|
|
|
$timezone = null, |
204
|
|
|
string $locale = null |
205
|
|
|
) { |
206
|
|
|
$year = is_null($year) ? date('Y') : $year; |
207
|
|
|
$month = is_null($month) ? date('m') : $month; |
208
|
|
|
$day = is_null($day) ? date('d') : $day; |
209
|
|
|
$hour = empty($hour) ? 0 : $hour; |
210
|
|
|
$minutes = empty($minutes) ? 0 : $minutes; |
211
|
|
|
$seconds = empty($seconds) ? 0 : $seconds; |
212
|
|
|
|
213
|
|
|
return static::parse(date('Y-m-d H:i:s', strtotime("{$year}-{$month}-{$day} {$hour}:{$minutes}:{$seconds}")), $timezone, $locale); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Provides a replacement for DateTime’s method of the same name. |
218
|
|
|
* This allows the timezone to be set at the same time, and returns |
219
|
|
|
* a Time instance, instead of DateTime. |
220
|
|
|
* |
221
|
|
|
* @param string $format |
222
|
|
|
* @param string $datetime |
223
|
|
|
* @param \DateTimeZone|string|null $timezone |
224
|
|
|
* |
225
|
|
|
* @return \Syscodes\Support\Chronos\Time |
226
|
|
|
*/ |
227
|
|
|
public static function createFromFormat($format, $datetime, $timezone = null) |
228
|
|
|
{ |
229
|
|
|
$date = parent::createFromFormat($format, $datetime); |
230
|
|
|
|
231
|
|
|
return static::parse($date->format('Y-m-d H:i:s'), $timezone); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns a new instance with the datetime set based on the provided UNIX timestamp. |
236
|
|
|
* |
237
|
|
|
* @param int $timestamp |
238
|
|
|
* @param string|null $timezone |
239
|
|
|
* @param string|null $locale |
240
|
|
|
* |
241
|
|
|
* @return \Syscodes\Support\Chronos\Time |
242
|
|
|
*/ |
243
|
|
|
public static function createFromTimestamp(int $timestamp, $timezone = null, string $locale = null) |
244
|
|
|
{ |
245
|
|
|
return static::parse(date('Y-m-d H:i:s', $timestamp), $timezone, $locale); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Takes an instance of DateTime and returns an instance |
250
|
|
|
* of Time with it's same values. |
251
|
|
|
* |
252
|
|
|
* @param \DateTime $datetime |
253
|
|
|
* @param string|null $locale |
254
|
|
|
* |
255
|
|
|
* @return \Syscodes\Support\Chronos\Time |
256
|
|
|
*/ |
257
|
|
|
public static function instance($datetime, string $locale = null) |
258
|
|
|
{ |
259
|
|
|
$date = $datetime->format('Y-m-d H:i:s'); |
260
|
|
|
$timezone = $datetime->getTimezone(); |
261
|
|
|
|
262
|
|
|
return static::parse($date, $timezone, $locale); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Creates an instance of Time that will be returned during testing |
267
|
|
|
* when calling 'Time::now' instead of the current time. |
268
|
|
|
* |
269
|
|
|
* @param \Syscodes\Support\Chronos\Time|string $datetime |
270
|
|
|
* @param string|null $timezone |
271
|
|
|
* @param string|null $locale |
272
|
|
|
* |
273
|
|
|
* @return static |
274
|
|
|
*/ |
275
|
|
|
public static function setTestNow($datetime = null, $timezone = null, string $locale = null) |
276
|
|
|
{ |
277
|
|
|
if (null === $datetime) { |
278
|
|
|
static::$testNow = null; |
|
|
|
|
279
|
|
|
|
280
|
|
|
return; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if (is_string($datetime)) { |
284
|
|
|
$time = static::parse($datetime, $timezone, $locale); |
285
|
|
|
} elseif ($datetime instanceof DateTime && ! $datetime instanceof static) { |
|
|
|
|
286
|
|
|
$time = static::parse($datetime->format('Y-m-d H:i:s'), $timezone); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
static::$testNow = $time; |
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns whether we have a testNow instance saved. |
294
|
|
|
* |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
|
|
public static function hasTestNow() |
298
|
|
|
{ |
299
|
|
|
return ! is_null(static::$testNow); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
// Difference |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get difference time depending on a specific period of time. |
306
|
|
|
* |
307
|
|
|
* @param string $time |
308
|
|
|
* @param string|null $timezone |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
|
|
public function difference($time, string $timezone = null) |
313
|
|
|
{ |
314
|
|
|
$testTime = $this->getConvertedUTC($time, $timezone); |
|
|
|
|
315
|
|
|
$ourTime = $this->getConvertedUTC($this); |
316
|
|
|
|
317
|
|
|
return $this->getDifferenceTime($ourTime, $testTime); |
|
|
|
|
318
|
|
|
} |
319
|
|
|
} |