|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Time; |
|
4
|
|
|
|
|
5
|
|
|
use Sfneal\Actions\AbstractService; |
|
6
|
|
|
|
|
7
|
|
|
class TimePeriods extends AbstractService |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Names of helper methods that don't return time periods. |
|
11
|
|
|
*/ |
|
12
|
|
|
const HELPER_METHODS = ['all', 'get', 'getPeriod', 'mapMethods']; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Retrieve an array time period names and time period ranges. |
|
16
|
|
|
* |
|
17
|
|
|
* @param array $exclusions |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function all(array $exclusions = []) |
|
21
|
|
|
{ |
|
22
|
|
|
// Get all of the methods in this class except 'all' & 'getPeriod' |
|
23
|
|
|
$methods = array_filter(get_class_methods(new self()), function ($method) use ($exclusions) { |
|
24
|
|
|
return ! in_array($method, array_unique(array_merge(self::HELPER_METHODS, $exclusions))); |
|
25
|
|
|
}); |
|
26
|
|
|
|
|
27
|
|
|
// Method name key, with method results values |
|
28
|
|
|
return self::mapMethods($methods); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Retrieve an array time period names and time period ranges. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $inclusions |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function get(...$inclusions) |
|
38
|
|
|
{ |
|
39
|
|
|
// Get all of the methods in this class except 'all' & 'getPeriod' |
|
40
|
|
|
$methods = array_filter(get_class_methods(new self()), function ($method) use ($inclusions) { |
|
41
|
|
|
return in_array($method, $inclusions); |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
// Method name key, with method results values |
|
45
|
|
|
return self::mapMethods($methods); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Retrieve a single period range. |
|
50
|
|
|
* |
|
51
|
|
|
* @param $period |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function timePeriod($period) |
|
55
|
|
|
{ |
|
56
|
|
|
return self::{$period}(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* This Month. |
|
61
|
|
|
* |
|
62
|
|
|
* @param $format string Date format |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function thisMonth(string $format = 'Y-m-d') |
|
66
|
|
|
{ |
|
67
|
|
|
return self::getPeriod( |
|
68
|
|
|
date($format, strtotime('first day of this month')).' 00:00:00', |
|
69
|
|
|
date($format, strtotime('last day of this month')).' 23:59:59', |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Last Month. |
|
75
|
|
|
* |
|
76
|
|
|
* @param $format string Date format |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function lastMonth(string $format = 'Y-m-d') |
|
80
|
|
|
{ |
|
81
|
|
|
return self::getPeriod( |
|
82
|
|
|
date($format, strtotime('first day of last month')).' 00:00:00', |
|
83
|
|
|
date($format, strtotime('last day of last month')).' 23:59:59', |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* This Week. |
|
89
|
|
|
* |
|
90
|
|
|
* @param $format string Date format |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function thisWeek(string $format = 'Y-m-d') |
|
94
|
|
|
{ |
|
95
|
|
|
return self::getPeriod( |
|
96
|
|
|
date($format, strtotime('Monday this week')).' 00:00:00', |
|
97
|
|
|
date($format, strtotime('Sunday this week')).' 23:59:59', |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Today. |
|
103
|
|
|
* |
|
104
|
|
|
* @param $format string Date format |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function today(string $format = 'Y-m-d') |
|
108
|
|
|
{ |
|
109
|
|
|
return self::getPeriod( |
|
110
|
|
|
date($format, strtotime('Today')).' 00:00:00', |
|
111
|
|
|
date($format, strtotime('Today')).' 23:59:59', |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Yesterday. |
|
117
|
|
|
* |
|
118
|
|
|
* @param $format string Date format |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function yesterday(string $format = 'Y-m-d') |
|
122
|
|
|
{ |
|
123
|
|
|
return self::getPeriod( |
|
124
|
|
|
date($format, strtotime('Yesterday')).' 00:00:00', |
|
125
|
|
|
date($format, strtotime('Yesterday')).' 23:59:59', |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Tomorrow. |
|
131
|
|
|
* |
|
132
|
|
|
* @param $format string Date format |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function tomorrow(string $format = 'Y-m-d') |
|
136
|
|
|
{ |
|
137
|
|
|
return self::getPeriod( |
|
138
|
|
|
date($format, strtotime('Tomorrow')).' 00:00:00', |
|
139
|
|
|
date($format, strtotime('Tomorrow')).' 23:59:59', |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Return an array of time periods with 'start' and 'end' keys. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $start |
|
147
|
|
|
* @param string $end |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
private static function getPeriod(string $start, string $end): array |
|
151
|
|
|
{ |
|
152
|
|
|
return [$start, $end]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Return method name key, with method results values. |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $methods |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
private static function mapMethods(array $methods) |
|
162
|
|
|
{ |
|
163
|
|
|
return arrayFlattenKeys(array_map(function ($method, $times) { |
|
164
|
|
|
return [$method => $times]; |
|
165
|
|
|
}, |
|
166
|
|
|
$methods, |
|
167
|
|
|
array_map(function ($method) { |
|
168
|
|
|
return self::{$method}(); |
|
169
|
|
|
}, $methods) |
|
170
|
|
|
), false); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|