1 | <?php |
||
2 | |||
3 | // +---------------------------------------------------------------------- |
||
4 | // | ThinkLibrary 6.0 for ThinkPhP 6.0 |
||
5 | // +---------------------------------------------------------------------- |
||
6 | // | 版权所有 2017~2020 [ https://www.dtapp.net ] |
||
7 | // +---------------------------------------------------------------------- |
||
8 | // | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
||
9 | // +---------------------------------------------------------------------- |
||
10 | // | 开源协议 ( https://mit-license.org ) |
||
11 | // +---------------------------------------------------------------------- |
||
12 | // | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
||
13 | // | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
||
14 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
||
15 | // +---------------------------------------------------------------------- |
||
16 | |||
17 | declare (strict_types=1); |
||
18 | |||
19 | namespace DtApp\ThinkLibrary\helper; |
||
20 | |||
21 | /** |
||
22 | * 时间管理类 |
||
23 | * @mixin Times |
||
24 | * @package DtApp\ThinkLibrary\helper |
||
25 | */ |
||
26 | class Times |
||
27 | { |
||
28 | /** |
||
29 | * 当前时间 |
||
30 | * @param string $format 格式 |
||
31 | * @return false|string |
||
32 | */ |
||
33 | public function getData(string $format = "Y-m-d H:i:s") |
||
34 | { |
||
35 | date_default_timezone_set('Asia/Shanghai'); |
||
36 | return date($format, time()); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * 当前时间戳 |
||
41 | * @return false|string |
||
42 | */ |
||
43 | public function getTime() |
||
44 | { |
||
45 | date_default_timezone_set('Asia/Shanghai'); |
||
46 | return time(); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * 毫秒时间 |
||
51 | * @return false|string |
||
52 | */ |
||
53 | public function getUDate() |
||
54 | { |
||
55 | date_default_timezone_set('Asia/Shanghai'); |
||
56 | $msec = 0; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
57 | list($msec, $sec) = explode(' ', microtime()); |
||
58 | return (float)sprintf('%.0f', ((float)$msec + (float)$sec) * 1000); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * 计算两个时间差 |
||
63 | * @param string $end_time 结束时间 |
||
64 | * @param string $start_time 开始时间 |
||
65 | * @return false|int |
||
66 | */ |
||
67 | public function getTimeDifference(string $end_time, string $start_time) |
||
68 | { |
||
69 | date_default_timezone_set('Asia/Shanghai'); |
||
70 | $end_time = strtotime($end_time); |
||
71 | $start_time = $start_time === '' ? time() : strtotime($start_time); |
||
72 | return $end_time - $start_time; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * 将指定日期转换为时间戳 |
||
77 | * @param string $date |
||
78 | * @return false|int |
||
79 | */ |
||
80 | public function dateToTimestamp(string $date) |
||
81 | { |
||
82 | date_default_timezone_set('Asia/Shanghai'); |
||
83 | return strtotime($date); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * 将指定时间戳转换为日期 |
||
88 | * @param string $format |
||
89 | * @param int $time |
||
90 | * @return false|string |
||
91 | */ |
||
92 | public function timestampToDate(int $time, string $format = "Y-m-d H:i:s") |
||
93 | { |
||
94 | date_default_timezone_set('Asia/Shanghai'); |
||
95 | return date($format, $time); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * 在某个时间之前的时间 |
||
100 | * @param string $format 格式 |
||
101 | * @param int $mun 多少秒 |
||
102 | * @param int $time |
||
103 | * @return false|string |
||
104 | */ |
||
105 | public function dateBefore(string $format = "Y-m-d H:i:s", int $mun = 60, int $time = 0) |
||
106 | { |
||
107 | date_default_timezone_set('Asia/Shanghai'); |
||
108 | if (empty($time)) { |
||
109 | $time = time(); |
||
110 | } |
||
111 | return date($format, $time - $mun); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * 在某个时间之后的时间 |
||
116 | * @param string $format 格式 |
||
117 | * @param int $mun 多少秒 |
||
118 | * @param int $time |
||
119 | * @return false|string |
||
120 | */ |
||
121 | public function dateRear(string $format = "Y-m-d H:i:s", int $mun = 60, int $time = 0) |
||
122 | { |
||
123 | date_default_timezone_set('Asia/Shanghai'); |
||
124 | if (empty($time)) { |
||
125 | $time = time(); |
||
126 | } |
||
127 | return date($format, $time + $mun); |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * 判断当前的时分是否在指定的时间段内 |
||
133 | * @param string $start 开始时间 |
||
134 | * @param string $end 结束时间 |
||
135 | * @return bool true:在范围内,false:没在范围内 |
||
136 | */ |
||
137 | public function checkIsBetweenTime(string $start, string $end) |
||
138 | { |
||
139 | date_default_timezone_set('Asia/Shanghai'); |
||
140 | $date = date('H:i'); |
||
141 | $curTime = strtotime($date);//当前时分 |
||
142 | $assignTime1 = strtotime($start);//获得指定分钟时间戳,00:00 |
||
143 | $assignTime2 = strtotime($end);//获得指定分钟时间戳,01:00 |
||
144 | $result = false; |
||
145 | if ($curTime > $assignTime1 && $curTime < $assignTime2) { |
||
146 | $result = true; |
||
147 | } |
||
148 | return $result; |
||
149 | } |
||
150 | } |
||
151 |