-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.php
602 lines (536 loc) · 14.6 KB
/
Time.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use DateTime;
use DateTimeZone;
use Tamedevelopers\Support\Str;
use Tamedevelopers\Support\Time;
use Tamedevelopers\Support\Country;
use Tamedevelopers\Support\Traits\TimeTrait;
use Tamedevelopers\Support\Capsule\TimeHelper;
class Time {
use TimeTrait;
/**
* For storing the time value
*
* @var mixed
* - int|string
*/
protected $date;
/**
* For storing the timezone value
*
* @var string
*/
protected $timezone;
/**
* static
*
* @var mixed
*/
static private $staticData;
/**
* Time constructor.
* @param int|string|null $date
* @param string|null $timezone
*/
public function __construct($date = null, $timezone = null)
{
if(empty($this->date)){
$this->date = TimeHelper::setPassedDate($date);
}
if(empty($this->timezone)){
$this->timezone = TimeHelper::setPassedTimezone($timezone);
}
// clone copy of self
if(!self::isTimeInstance()){
self::$staticData = clone $this;
}
}
/**
* Handle the calls to non-existent instance methods.
* @param string $name
* @param mixed $args
*
* @return mixed
*/
public function __call($name, $args)
{
return self::nonExistMethod($name, $args, $this);
}
/**
* Handle the calls to non-existent static methods.
* @param string $name
* @param mixed $args
*
* @return mixed
*/
static public function __callStatic($name, $args)
{
return self::nonExistMethod($name, $args, self::$staticData);
}
/**
* Set custom time
* @param int|string $date
* @return $this
*/
public function date($date)
{
return $this->__setDate($date);
}
/**
* Add Second to curent date
* @param int $value
* @return $this
*/
public function addSeconds($value = 0)
{
return $this->buildTimeModifier('second', $value);
}
/**
* Substract Second from curent date
* @param int $value
* @return $this
*/
public function subSeconds($value = 0)
{
return $this->buildTimeModifier('second', $value, true);
}
/**
* Add Minutes to curent date
* @param int $value
* @return $this
*/
public function addMinutes($value = 0)
{
return $this->buildTimeModifier('minute', $value);
}
/**
* Substract Minutes from curent date
* @param int $value
* @return $this
*/
public function subMinutes($value = 0)
{
return $this->buildTimeModifier('minute', $value, true);
}
/**
* Add Hours to curent date
* @param int $value
* @return $this
*/
public function addHours($value = 0)
{
return $this->buildTimeModifier('hour', $value);
}
/**
* Substract Hours from curent date
* @param int $value
* @return $this
*/
public function subHours($value = 0)
{
return $this->buildTimeModifier('hour', $value, true);
}
/**
* Add days to curent date
* @param int $value
* @return $this
*/
public function addDays($value = 0)
{
return $this->buildTimeModifier('day', $value);
}
/**
* Substract days from curent date
* @param int $value
* @return $this
*/
public function subDays($value = 0)
{
return $this->buildTimeModifier('day', $value, true);
}
/**
* Add Week to curent date
* @param int $value
* @return $this
*/
public function addWeeks($value = 0)
{
return $this->buildTimeModifier('week', $value);
}
/**
* Substract Week from curent date
* @param int $value
* @return $this
*/
public function subWeeks($value = 0)
{
return $this->buildTimeModifier('week', $value, true);
}
/**
* Add Month to curent date
* @param int $value
* @return $this
*/
public function addMonths($value = 0)
{
return $this->buildTimeModifier('month', $value);
}
/**
* Substract Month from curent date
* @param int $value
* @return $this
*/
public function subMonths($value = 0)
{
return $this->buildTimeModifier('month', $value, true);
}
/**
* Add Year to curent date
* @param int $value
* @return $this
*/
public function addYears($value = 0)
{
return $this->buildTimeModifier('year', $value);
}
/**
* Substract Year from curent date
* @param int $value
* @return $this
*/
public function subYears($value = 0)
{
return $this->buildTimeModifier('year', $value, true);
}
/**
* Set time to `now`
*
* @return $this
*/
public function now()
{
return $this->__setDate('now');
}
/**
* Set time to `today`
*
* @return $this
*/
public function today()
{
return $this->__setDate('today');
}
/**
* Set time to `yesterday`
*
* @return $this
*/
public function yesterday()
{
return $this->__setDate('yesterday');
}
/**
* Format time input
*
* @param string|null $format
* - Your defined format type i.e: Y-m-d H:i:s a
*
* @param int|string $date
* - string|int|float
*
* @return string
*/
public function __format($format = null, $date = null)
{
if(!empty($date)){
$this->__setDate($date);
}
if(empty($format)){
$format = "Y-m-d H:i:s";
}
return date($format, $this->date);
}
/**
* toDateTimeString
*
* @return string
*/
public function toDateTimeString()
{
return date('Y-m-d H:i:s', $this->date);
}
/**
* Create timestamp
*
* @param int|string $date
* - string|int|float
*
* @param string $format
* - Your defined format type i.e: Y-m-d H:i:s a
* - Converted TimeStamp
*
* @return string
*/
static public function timestamp($date, $format = "Y-m-d H:i:s")
{
$date = TimeHelper::setPassedDate($date);
return date($format, $date);
}
/**
* Create Javascript timer
*
* @param string|int $date
* - Converted TimeStamp
*
* @return string
*/
static public function toJsTimer($date)
{
return self::timestamp($date, 'M j, Y H:i:s');
}
/**
* Format a date range.
*
* @param string $value The range in the format "1-7" (days from today).
* @param string $format The desired date format, default is 'D, M j'.
*
* @return Tamedevelopers\Support\Capsule\TimeHelper
* - The formatted date, e.g., "Mon, May 27".
*/
public function dateRange($value, $format = 'D, M j')
{
// Check if the range has a hyphen
if (strpos($value, '-') !== false) {
// Split the range into start and end days
[$start, $end] = explode('-', $value);
} else {
[$start, $end] = [0, $value];
}
// Ensure the end value is the maximum number of days
$daysToStart = (int) Str::trim($start);
$daysToAdd = (int) Str::trim($end);
// Create a DateTime object for the current date
$startDate = $this->today()->addDays($daysToStart);
$endDate = $this->today()->addDays($daysToAdd);
return new TimeHelper($startDate, $endDate, $format);
}
/**
* Set the configuration options for text representations.
*
* @param array|null $options
*
* @return void
*/
static public function config(?array $options = [])
{
if(!defined('TAME_TIME_CONFIG')){
define('TAME_TIME_CONFIG', array_merge([
'night' => 'Good night!',
'morning' => 'Good morning!',
'afternoon' => 'Good afternoon!',
'evening' => 'Good evening!',
'now' => 'Just now',
's' => 's',
'd' => 'd',
'h' => 'h',
'm' => 'm',
'w' => 'w',
'y' => 'y',
'at' => 'at',
'ago' => 'ago',
'sec' => 'second',
'min' => 'minute',
'hour' => 'hour',
'year' => 'year',
'yesterday' => 'Yesterday',
], $options));
}
}
/**
* Get the number of seconds between the stored time and the current time.
* @return mixed
*/
public function __getSecond()
{
return $this->__timeDifference('sec');
}
/**
* Get the number of minutes between the stored time and the current time.
* @return mixed
*/
public function __getMin()
{
return $this->__timeDifference('mins');
}
/**
* Get the number of hours between the stored time and the current time.
* @return mixed
*/
public function __getHour()
{
return $this->__timeDifference('hour');
}
/**
* Get the number of days between the stored time and the current time.
* @return mixed
*/
public function __getDay()
{
return $this->__timeDifference('days');
}
/**
* Get the number of weeks between the stored time and the current time.
* @return mixed
*/
public function __getWeek()
{
$days = $this->__timeDifference('days');
return (int) floor($days / 7);
}
/**
* Get the number of months between the stored time and the current time.
* @return mixed
*/
public function __getMonth()
{
return $this->__timeDifference('month');
}
/**
* Get the number of years between the stored time and the current time.
* @return mixed
*/
public function __getYear()
{
return $this->__timeDifference('year');
}
/**
* Calculate the time difference between the stored time and the current time.
* @param string|null $mode
*
* @return mixed
*/
public function __timeDifference($mode = null)
{
$now = new DateTime('now', new DateTimeZone($this->timezone));
$date = new DateTime();
$date->setTimestamp(TimeHelper::carbonInstance($this->date));
// get difference
$difference = $now->diff($date);
$timeData = [
'year' => $difference->y,
'month' => ($difference->y * 12) + $difference->m,
'hour' => $difference->h,
'mins' => $difference->i,
'sec' => $difference->s,
'days' => $difference->days, //total number of days
];
return $timeData[$mode] ?? $timeData;
}
/**
* Get a greeting based on the current time.
* @param string|int $date
*
* @return string
*/
public function __greeting($date = 'now')
{
$dateTime = new DateTime();
$dateTime->setTimestamp(
TimeHelper::setPassedDate($date)
);
$now = new DateTime($dateTime->format('M d Y H:i:s'), new DateTimeZone($this->timezone));
$hour = (int) $now->format('H');
$text = self::getText();
if ($hour >= 0 && $hour < 12) {
return $text['morning'];
} elseif ($hour >= 12 && $hour < 17) {
return $text['afternoon'];
} elseif ($hour >= 17 && $hour < 20) {
return $text['evening'];
}
return $text['night'];
}
/**
* Get a time ago representation based on the time difference.
* @param string|null $mode
* - [optional] int|short|full
*
* @return string
*/
public function __timeAgo($mode = null)
{
$minutes = $this->__getMin();
$seconds = $this->__getSecond();
$hours = $this->__getHour();
$days = $this->__getDay();
$weeks = $this->__getWeek();
$years = $this->__getYear();
$date = $this->__getDate();
$text = self::getText();
if ($days === 0 && $hours === 0 && $minutes < 1) {
$data = [
'full' => $text['now'],
'short' => $text['now'],
'duration' => $seconds,
];
} elseif ($days === 0 && $hours === 0) {
$data = [
'full' => "{$minutes} {$text['min']}" . ($minutes > 1 ? $text['s'] : '') . " {$text['ago']}",
'short' => "{$minutes}{$text['m']}",
'duration' => $minutes,
];
} elseif ($days === 0 && $hours < 24) {
$data = [
'full' => "{$hours} {$text['hour']}" . ($hours > 1 ? $text['s'] : '') . " {$text['ago']}",
'short' => "{$hours}{$text['h']}",
'duration' => $hours,
];
} elseif ($days < 7) {
// create default
$fullText = str_replace('**', $text['at'], date("D ** h:m a", $date));
if($days === 1){
$fullText = "{$text['yesterday']} {$text['at']} " . date("h:m a", $date);
}
$data = [
'full' => $fullText,
'short' => "{$days}{$text['d']}",
'duration' => $days,
];
} elseif ($years > 0) {
$data = [
'full' => "{$years} {$text['year']}" . ($years > 1 ? $text['s'] : '') . " {$text['ago']}",
'short' => "{$years}{$text['y']}",
'duration' => $years,
];
} else {
$data = [
'full' => str_replace('**', $text['at'], date("d M ** h:m a", $date)),
'short' => "{$weeks}{$text['w']}",
'duration' => $weeks,
];
}
// merge
$data = array_merge($data, [
'time' => $date,
'date' => date('d M, Y', $date),
'date_time' => date('d M, Y h:ma', $date),
'time_stamp'=> date('M j, Y H:i:s', $date)
]);
return $data[$mode] ?? $data;
}
/**
* Get the text representation options.
* @param string|null $mode
*
* @return mixed
*/
static private function getText($mode = null)
{
if(!defined('TAME_TIME_CONFIG')){
self::config();
}
return TAME_TIME_CONFIG[$mode] ?? TAME_TIME_CONFIG;
}
}