Total Complexity | 14 |
Total Lines | 43 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
14 | class Date |
||
15 | { |
||
16 | /** |
||
17 | * 生成Carbon对象,不合法数据会返回默认值 |
||
18 | * |
||
19 | * @param $dateTime |
||
20 | * @param mixed $default |
||
21 | * @return Carbon |
||
22 | */ |
||
23 | 3 | public static function toCarbon($dateTime = null, $default = false) |
|
24 | { |
||
25 | 3 | if ($dateTime instanceof Carbon) { |
|
26 | 3 | return $dateTime; |
|
27 | } |
||
28 | 3 | $default = empty($default) ? Carbon::now() : $default; |
|
29 | 3 | if (strtotime($dateTime) > 0) { |
|
30 | 3 | return Carbon::parse($dateTime); |
|
31 | } |
||
32 | |||
33 | 3 | if (is_numeric($dateTime)) { |
|
34 | 3 | return Carbon::createFromTimestamp($dateTime); |
|
35 | } |
||
36 | |||
37 | 3 | return $default; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * 人性化显示两个时间的差 |
||
42 | * @param DateTime $leftTime |
||
43 | * @param DateTime $rightTime |
||
44 | * @param bool $absolute |
||
45 | * @return string |
||
46 | */ |
||
47 | 3 | public static function timeDiffFormat(DateTime $leftTime, DateTime $rightTime, $absolute = false): string |
|
57 | } |
||
58 | } |
||
59 |