1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Time; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
class Carbonate |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Create a Carbon datetime objects $x days forward/backward in the past or future. |
11
|
|
|
* |
12
|
|
|
* - a positive(+) integer $days value correlates to days FORWARD |
13
|
|
|
* - a negative(-) integer $days value correlates to days BACKWARD |
14
|
|
|
* |
15
|
|
|
* @param int $days |
16
|
|
|
* @return Carbon |
17
|
|
|
*/ |
18
|
|
|
public static function days(int $days): Carbon |
19
|
|
|
{ |
20
|
|
|
return ($days < 0) ? self::daysAgo(abs($days)) : self::daysHence($days); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a Carbon datetime objects $x months forward/backward in the past or future. |
25
|
|
|
* |
26
|
|
|
* - a positive(+) integer $months value correlates to days FORWARD |
27
|
|
|
* - a negative(-) integer $months value correlates to days BACKWARD |
28
|
|
|
* |
29
|
|
|
* @param int $months |
30
|
|
|
* @return Carbon |
31
|
|
|
*/ |
32
|
|
|
public static function months(int $months): Carbon |
33
|
|
|
{ |
34
|
|
|
return ($months < 0) ? self::monthsAgo(abs($months)) : self::monthsHence($months); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a Carbon datetime objects $x years forward/backward in the past or future. |
39
|
|
|
* |
40
|
|
|
* - a positive(+) integer $years value correlates to days FORWARD |
41
|
|
|
* - a negative(-) integer $years value correlates to days BACKWARD |
42
|
|
|
* |
43
|
|
|
* @param int $years |
44
|
|
|
* @return Carbon |
45
|
|
|
*/ |
46
|
|
|
public static function years(int $years): Carbon |
47
|
|
|
{ |
48
|
|
|
return ($years < 0) ? self::yearsAgo(abs($years)) : self::yearsHence($years); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a Carbon datetime object representing $x days ago. |
53
|
|
|
* |
54
|
|
|
* @param int $days |
55
|
|
|
* @return Carbon |
56
|
|
|
*/ |
57
|
|
|
public static function daysAgo(int $days): Carbon |
58
|
|
|
{ |
59
|
|
|
return Carbon::now()->subDays($days); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create a Carbon datetime object representing $x days from now. |
64
|
|
|
* |
65
|
|
|
* @param int $days |
66
|
|
|
* @return Carbon |
67
|
|
|
*/ |
68
|
|
|
public static function daysHence(int $days): Carbon |
69
|
|
|
{ |
70
|
|
|
return Carbon::now()->addDays($days); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create a Carbon datetime object representing $x months ago. |
75
|
|
|
* |
76
|
|
|
* @param int $months |
77
|
|
|
* @return Carbon |
78
|
|
|
*/ |
79
|
|
|
public static function monthsAgo(int $months): Carbon |
80
|
|
|
{ |
81
|
|
|
return Carbon::now()->subMonths($months); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a Carbon datetime object representing $x months from now. |
86
|
|
|
* |
87
|
|
|
* @param int $months |
88
|
|
|
* @return Carbon |
89
|
|
|
*/ |
90
|
|
|
public static function monthsHence(int $months): Carbon |
91
|
|
|
{ |
92
|
|
|
return Carbon::now()->addMonths($months); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create a Carbon datetime object representing $x days ago. |
97
|
|
|
* |
98
|
|
|
* @param int $years |
99
|
|
|
* @return Carbon |
100
|
|
|
*/ |
101
|
|
|
public static function yearsAgo(int $years): Carbon |
102
|
|
|
{ |
103
|
|
|
return Carbon::now()->subYears($years); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create a Carbon datetime object representing $x days from now. |
108
|
|
|
* |
109
|
|
|
* @param int $years |
110
|
|
|
* @return Carbon |
111
|
|
|
*/ |
112
|
|
|
public static function yearsHence(int $years): Carbon |
113
|
|
|
{ |
114
|
|
|
return Carbon::now()->addYears($years); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|