Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Carbon | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
createFromDatetime | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
createTimestampFromDatetime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Helpers; |
6 | |
7 | class Carbon extends \Carbon\Carbon |
8 | { |
9 | public const DATETIME_LOCAL = '!Y-m-d\TH:i'; |
10 | |
11 | public const DATETIME_FALLBACK = '!Y-m-d H:i'; |
12 | |
13 | public const DATETIME_FORMATS = [ |
14 | self::DATETIME_LOCAL, |
15 | self::DATETIME_FALLBACK, |
16 | ]; |
17 | |
18 | /** |
19 | * Parses HTML datetime-local and ISO date/time strings. |
20 | * |
21 | * @return self|null Carbon if parseable, else null |
22 | * @see self::DATETIME_FORMATS |
23 | */ |
24 | public static function createFromDatetime(string $value): ?\Carbon\Carbon |
25 | { |
26 | foreach (self::DATETIME_FORMATS as $datetimeFormat) { |
27 | if (self::canBeCreatedFromFormat($value, $datetimeFormat)) { |
28 | return self::createFromFormat($datetimeFormat, $value); |
29 | } |
30 | } |
31 | |
32 | return null; |
33 | } |
34 | |
35 | /** |
36 | * Parses HTML datetime-local and ISO date/time strings. |
37 | * |
38 | * @return int|null Timestamp if parseable, else null |
39 | * @see self::DATETIME_FORMATS |
40 | */ |
41 | public static function createTimestampFromDatetime(string $value): ?int |
42 | { |
43 | $carbon = self::createFromDateTime($value); |
44 | return $carbon?->timestamp; |
45 | } |
46 | } |