This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Scabbia2 Helpers Component |
||
4 | * https://github.com/eserozvataf/scabbia2 |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | * |
||
9 | * @link https://github.com/eserozvataf/scabbia2-helpers for the canonical source repository |
||
10 | * @copyright 2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/) |
||
11 | * @license http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0 |
||
12 | */ |
||
13 | |||
14 | namespace Scabbia\Helpers; |
||
15 | |||
16 | /** |
||
17 | * A bunch of utility methods for date and time operations |
||
18 | * |
||
19 | * @package Scabbia\Helpers |
||
20 | * @author Eser Ozvataf <[email protected]> |
||
21 | * @since 1.0.0 |
||
22 | * |
||
23 | * @scabbia-compile |
||
24 | * |
||
25 | * @todo improve humanize (fuzzy span) |
||
26 | */ |
||
27 | class Date |
||
28 | { |
||
29 | /** |
||
30 | * Default variables for Date utility set |
||
31 | * |
||
32 | * @type array $defaults array of default variables |
||
33 | */ |
||
34 | public static $defaults = [ |
||
35 | "short_date" => "d.m.Y", |
||
36 | "short_time" => "H:i", |
||
37 | "short_time_with_seconds" => "H:i:s", |
||
38 | "short_datetime" => "d.m.Y H:i", |
||
39 | "short_datetime_with_seconds" => "d.m.Y H:i:s", |
||
40 | |||
41 | "seconds" => "seconds", |
||
42 | "minutes" => "minutes", |
||
43 | "hours" => "hours", |
||
44 | "days" => "days", |
||
45 | "weeks" => "weeks", |
||
46 | "months" => "months", |
||
47 | |||
48 | "yesterday" => "Yesterday", |
||
49 | "today" => "Today", |
||
50 | "tomorrow" => "Tomorrow", |
||
51 | |||
52 | "now" => "Now" |
||
53 | ]; |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Constructor to prevent new instances of Date class |
||
58 | * |
||
59 | * @return Date |
||
0 ignored issues
–
show
|
|||
60 | */ |
||
61 | final private function __construct() |
||
62 | { |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Clone method to prevent duplication of Date class |
||
67 | * |
||
68 | * @return Date |
||
69 | */ |
||
70 | final private function __clone() |
||
71 | { |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Unserialization method to prevent restoration of Date class |
||
76 | * |
||
77 | * @return Date |
||
78 | */ |
||
79 | final private function __wakeup() |
||
80 | { |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Sets the default variables |
||
85 | * |
||
86 | * @param array $uDefaults variables to be set |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | public static function setDefaults($uDefaults) |
||
91 | { |
||
92 | self::$defaults = $uDefaults + self::$defaults; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Timestamp of beginning of the day |
||
97 | * |
||
98 | * @param int $uTimestamp timestamp |
||
99 | * |
||
100 | * @return int timestamp of beginning of the day |
||
101 | */ |
||
102 | public static function beginningOfDay($uTimestamp) |
||
103 | { |
||
104 | return mktime(0, 0, 0, date("m", $uTimestamp), date("d", $uTimestamp), date("Y", $uTimestamp)); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Transforms given period into a time span |
||
109 | * |
||
110 | * @param int $uPeriod period |
||
111 | * @param bool $uNoDays return null if it passed a day |
||
112 | * |
||
113 | * @return null|array value and unit |
||
114 | */ |
||
115 | public static function ago($uPeriod, $uNoDays = false) |
||
116 | { |
||
117 | if (!$uNoDays) { |
||
118 | if ($uPeriod >= 2592000) { |
||
119 | return [ceil($uPeriod / 2592000), self::$constant["months"]]; |
||
120 | } |
||
121 | |||
122 | if ($uPeriod >= 604800) { |
||
123 | return [ceil($uPeriod / 604800), self::$constant["weeks"]]; |
||
124 | } |
||
125 | |||
126 | View Code Duplication | if ($uPeriod >= 86400) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
127 | return [ceil($uPeriod / 86400), self::$constant["days"]]; |
||
128 | } |
||
129 | } elseif ($uPeriod >= 86400) { |
||
130 | return null; |
||
131 | } |
||
132 | |||
133 | if ($uPeriod >= 3600) { |
||
134 | return [ceil($uPeriod / 3600), self::$constant["hours"]]; |
||
135 | } |
||
136 | |||
137 | View Code Duplication | if ($uPeriod >= 60) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
138 | return [ceil($uPeriod / 60), self::$constant["minutes"]]; |
||
139 | } |
||
140 | |||
141 | if ($uPeriod > 0) { |
||
142 | return [ceil($uPeriod), self::$constant["seconds"]]; |
||
143 | } |
||
144 | |||
145 | return null; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Transforms a timestamp to human-readable format |
||
150 | * |
||
151 | * @param int $uTimestamp timestamp |
||
152 | * @param int $uConstantTimestamp constant timestamp |
||
153 | * @param bool $uCalculateAgo uses ago method for past |
||
154 | * @param bool $uShowHours includes hour and minute |
||
155 | * |
||
156 | * @return string output |
||
157 | */ |
||
158 | public static function humanize($uTimestamp, $uConstantTimestamp, $uCalculateAgo = true, $uShowHours = true) |
||
159 | { |
||
160 | if (($tDifference = $uConstantTimestamp - $uTimestamp) >= 0 && $uCalculateAgo) { |
||
161 | $tAgo = self::ago($tDifference, true); |
||
162 | |||
163 | if ($tAgo !== null) { |
||
164 | return implode(" ", $tAgo); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | View Code Duplication | if ($tDifference >= 86400) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
169 | if ($uShowHours) { |
||
170 | return self::$defaults["yesterday"] . ", " . date(self::$defaults["short_time"], $uTimestamp); |
||
171 | } |
||
172 | |||
173 | return self::$defaults["yesterday"]; |
||
174 | } |
||
175 | |||
176 | View Code Duplication | if ($tDifference > 0) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
177 | if ($uShowHours) { |
||
178 | return self::$defaults["today"] . ", " . date(self::$defaults["short_time"], $uTimestamp); |
||
179 | } |
||
180 | |||
181 | return self::$defaults["today"]; |
||
182 | } |
||
183 | |||
184 | if ($tDifference === 0) { |
||
185 | return self::$defaults["now"]; |
||
186 | } |
||
187 | |||
188 | View Code Duplication | if ($tDifference >= -86400) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
189 | if ($uShowHours) { |
||
190 | return self::$defaults["tomorrow"] . ", " . date(self::$defaults["short_time"], $uTimestamp); |
||
191 | } |
||
192 | |||
193 | return self::$defaults["tomorrow"]; |
||
194 | } |
||
195 | |||
196 | if ($uShowHours) { |
||
197 | return date(self::$defaults["short_datetime"], $uTimestamp); |
||
198 | } |
||
199 | |||
200 | return date(self::$defaults["short_date"], $uTimestamp); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Transforms a timestamp to GMT format |
||
205 | * |
||
206 | * @param int $uTimestamp timestamp |
||
207 | * @param bool $uAddSuffix adds GMT at the end of the output |
||
208 | * |
||
209 | * @return string output |
||
210 | */ |
||
211 | public static function toGmt($uTimestamp, $uAddSuffix = true) |
||
212 | { |
||
213 | if ($uAddSuffix) { |
||
214 | return gmdate("D, d M Y H:i:s", $uTimestamp) . " GMT"; |
||
215 | } |
||
216 | |||
217 | return gmdate("D, d M Y H:i:s", $uTimestamp); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Transforms a timestamp to DOS format |
||
222 | * |
||
223 | * @param int $uTimestamp timestamp |
||
224 | * |
||
225 | * @return string output |
||
226 | */ |
||
227 | public static function toDos($uTimestamp) |
||
228 | { |
||
229 | $tTimeArray = getdate($uTimestamp); |
||
230 | |||
231 | if ($tTimeArray["year"] < 1980) { |
||
232 | $tTimeArray["year"] = 1980; |
||
233 | $tTimeArray["mon"] = 1; |
||
234 | $tTimeArray["mday"] = 1; |
||
235 | $tTimeArray["hours"] = 0; |
||
236 | $tTimeArray["minutes"] = 0; |
||
237 | $tTimeArray["seconds"] = 0; |
||
238 | } |
||
239 | |||
240 | // 4byte: hi=date, lo=time |
||
241 | return (($tTimeArray["year"] - 1980) << 25) | |
||
242 | ($tTimeArray["mon"] << 21) | |
||
243 | ($tTimeArray["mday"] << 16) | |
||
244 | ($tTimeArray["hours"] << 11) | |
||
245 | ($tTimeArray["minutes"] << 5) | |
||
246 | ($tTimeArray["seconds"] >> 1); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Transforms a timestamp from GMT format |
||
251 | * |
||
252 | * @param int $uTimestamp timestamp |
||
253 | * |
||
254 | * @return string output |
||
255 | */ |
||
256 | public static function fromDos($uTimestamp) |
||
257 | { |
||
258 | return mktime( |
||
259 | ($uTimestamp >> 11) & 0x1f, |
||
260 | ($uTimestamp >> 5) & 0x3f, |
||
261 | 2 * ($uTimestamp & 0x1f), |
||
262 | ($uTimestamp >> 21) & 0x0f, |
||
263 | ($uTimestamp >> 16) & 0x1f, |
||
264 | (($uTimestamp >> 25) & 0x7f) + 1980 |
||
265 | ); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Transforms a timestamp to database format |
||
270 | * |
||
271 | * @param int $uTimestamp timestamp |
||
272 | * @param string $uFormat destination format |
||
273 | * |
||
274 | * @return string output |
||
275 | */ |
||
276 | View Code Duplication | public static function toDb($uTimestamp, $uFormat = "d-m-Y H:i:s") |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
277 | { |
||
278 | if (!is_numeric($uTimestamp)) { |
||
279 | $tTime = date_parse_from_format($uFormat, $uTimestamp); |
||
280 | $uTimestamp = mktime( |
||
281 | $tTime["hour"], |
||
282 | $tTime["minute"], |
||
283 | $tTime["second"], |
||
284 | $tTime["month"], |
||
285 | $tTime["day"], |
||
286 | $tTime["year"] |
||
287 | ); // $tTime["is_dst"] |
||
288 | } |
||
289 | |||
290 | return date("Y-m-d H:i:s", $uTimestamp); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Transforms a timestamp from database format |
||
295 | * |
||
296 | * @param int $uTimestamp timestamp |
||
297 | * @param string $uFormat source format |
||
298 | * |
||
299 | * @return string output |
||
300 | */ |
||
301 | public static function fromDb($uTimestamp, $uFormat = "d-m-Y H:i:s") |
||
302 | { |
||
303 | $tTime = date_parse_from_format($uFormat, $uTimestamp); |
||
304 | |||
305 | return mktime( |
||
306 | $tTime["hour"], |
||
307 | $tTime["minute"], |
||
308 | $tTime["second"], |
||
309 | $tTime["month"], |
||
310 | $tTime["day"], |
||
311 | $tTime["year"] |
||
312 | ); // $tTime["is_dst"] |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Transforms a timestamp into another format |
||
317 | * |
||
318 | * @param int $uTimestamp timestamp |
||
319 | * @param string $uSourceFormat source format |
||
320 | * @param string $uDestinationFormat destination format |
||
321 | * |
||
322 | * @return string output |
||
323 | */ |
||
324 | View Code Duplication | public static function convert($uTimestamp, $uSourceFormat, $uDestinationFormat) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
325 | { |
||
326 | $tTime = date_parse_from_format($uSourceFormat, $uTimestamp); |
||
327 | $tTimestamp = mktime( |
||
328 | $tTime["hour"], |
||
329 | $tTime["minute"], |
||
330 | $tTime["second"], |
||
331 | $tTime["month"], |
||
332 | $tTime["day"], |
||
333 | $tTime["year"] |
||
334 | ); // $tTime["is_dst"] |
||
335 | |||
336 | return date($uDestinationFormat, $tTimestamp); |
||
337 | } |
||
338 | } |
||
339 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.