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