1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class DateTime |
4
|
|
|
* |
5
|
|
|
* @link https://www.icy2003.com/ |
6
|
|
|
* @author icy2003 <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace icy2003\php\ihelpers; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* 日期类 |
14
|
|
|
*/ |
15
|
|
|
class DateTime |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* 构造函数 |
20
|
|
|
* |
21
|
|
|
* @param string $timezone 时区设置,默认上海 |
22
|
|
|
*/ |
23
|
12 |
|
public function __construct($timezone = 'Asia/Shanghai') |
24
|
|
|
{ |
25
|
12 |
|
date_default_timezone_set($timezone); |
26
|
12 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 距离今天偏移量天数的开始和结束时间戳 |
30
|
|
|
* |
31
|
|
|
* - 0:今天,1:明天,-1:昨天,以此类推 |
32
|
|
|
* |
33
|
|
|
* @param integer $offset 天数偏移量,默认 0,即今天 |
34
|
|
|
* @param integer|null $time 设定该时间戳为今天 |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
1 |
|
public function dayRange($offset = 0, $time = null) |
39
|
|
|
{ |
40
|
1 |
|
null === $time && $time = time(); |
41
|
1 |
|
$y = (int) (date('Y', $time)); |
42
|
1 |
|
$m = (int) (date('m', $time)); |
43
|
1 |
|
$d = (int) (date('d', $time) + $offset); |
44
|
|
|
return [ |
45
|
1 |
|
mktime(0, 0, 0, $m, $d, $y), |
46
|
1 |
|
mktime(23, 59, 59, $m, $d, $y), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 距离本周偏移量周数的开始和结束的时间戳 |
52
|
|
|
* |
53
|
|
|
* - 星期天是第一天,星期六是最后一天!! |
54
|
|
|
* |
55
|
|
|
* @param integer $offset |
56
|
|
|
* @param integer|null $time 设定该时间戳为本周 |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
1 |
|
public function weekRange($offset = 0, $time = null) |
61
|
|
|
{ |
62
|
1 |
|
null === $time && $time = time(); |
63
|
1 |
|
$offset = (int) $offset; |
64
|
|
|
return [ |
65
|
1 |
|
strtotime(date('Y-m-d', strtotime('Sunday ' . ($offset - 1) . ' week', $time))), |
66
|
1 |
|
strtotime(date('Y-m-d', strtotime('Saturday ' . $offset . ' week', $time))) + 24 * 3600 - 1, |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* 距离本月偏移量月份数的开始和结束的时间戳 |
72
|
|
|
* |
73
|
|
|
* @param integer $offset |
74
|
|
|
* @param integer|null $time 设定该时间戳为本月 |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
1 |
|
public function monthRange($offset = 0, $time = null) |
79
|
|
|
{ |
80
|
1 |
|
null === $time && $time = time(); |
81
|
1 |
|
$y = (int) (date('Y', $time)); |
82
|
1 |
|
$m = (int) (date('m', $time) + $offset); |
83
|
1 |
|
$begin = mktime(0, 0, 0, $m, 1, $y); |
84
|
1 |
|
$end = mktime(23, 59, 59, $m, (int) date('t', $begin), $y); |
85
|
|
|
|
86
|
1 |
|
return [$begin, $end]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 距离今年偏移量年份数的开始和结束的时间戳 |
91
|
|
|
* |
92
|
|
|
* @param integer $offset |
93
|
|
|
* @param integer|null $time 设定该时间戳为本年 |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
1 |
|
public function yearRange($offset = 0, $time = null) |
98
|
|
|
{ |
99
|
1 |
|
null === $time && $time = time(); |
100
|
1 |
|
$year = (int) (date('Y', $time) + $offset); |
101
|
|
|
return [ |
102
|
1 |
|
mktime(0, 0, 0, 1, 1, $year), |
103
|
1 |
|
mktime(23, 59, 59, 12, 31, $year), |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* 距离某时间偏移量天数的时间戳 |
109
|
|
|
* |
110
|
|
|
* @param integer $offset |
111
|
|
|
* @param integer|null $time 不给则取当前时间 |
112
|
|
|
* |
113
|
|
|
* @return integer |
114
|
|
|
*/ |
115
|
1 |
|
public function day($offset = 0, $time = null) |
116
|
|
|
{ |
117
|
1 |
|
null === $time && $time = time(); |
118
|
1 |
|
$offset = (int) $offset; |
119
|
1 |
|
return $time + 3600 * 24 * $offset; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 距离某时间偏移量星期数的时间戳 |
124
|
|
|
* |
125
|
|
|
* @param integer $offset |
126
|
|
|
* @param integer|null $time 不给则取当前时间 |
127
|
|
|
* |
128
|
|
|
* @return integer |
129
|
|
|
*/ |
130
|
1 |
|
public function week($offset = 0, $time = null) |
131
|
|
|
{ |
132
|
1 |
|
null === $time && $time = time(); |
133
|
1 |
|
$offset = (int) $offset; |
134
|
1 |
|
return $time + 3600 * 24 * 7 * $offset; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* 距离某时间偏移量月份数的时间戳 |
139
|
|
|
* |
140
|
|
|
* @param integer $offset |
141
|
|
|
* @param integer|null $time 不给则取当前时间 |
142
|
|
|
* |
143
|
|
|
* @return integer |
144
|
|
|
*/ |
145
|
1 |
|
public function month($offset = 0, $time = null) |
146
|
|
|
{ |
147
|
1 |
|
null === $time && $time = time(); |
148
|
1 |
|
$offset = (int) $offset; |
149
|
1 |
|
return $time + 3600 * 24 * date('t', $time) * $offset; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* 距离某时间偏移量年份数的时间戳 |
154
|
|
|
* |
155
|
|
|
* @param integer $offset |
156
|
|
|
* @param integer|null $time 不给则取当前时间 |
157
|
|
|
* |
158
|
|
|
* @return integer |
159
|
|
|
*/ |
160
|
1 |
|
public function year($offset = 0, $time = null) |
161
|
|
|
{ |
162
|
1 |
|
null === $time && $time = time(); |
163
|
1 |
|
$offset = (int) $offset; |
|
|
|
|
164
|
1 |
|
return mktime((int) date('H', $time), (int) date('i', $time), (int) date('s', $time), (int) date('m', $time), (int) date('d', $time), (int) date('Y', $time) + 1); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* 返回星期几的英文名 |
169
|
|
|
* |
170
|
|
|
* @param integer|null $time |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
1 |
|
public function weekName($time = null) |
175
|
|
|
{ |
176
|
1 |
|
null === $time && $time = time(); |
177
|
1 |
|
return date('l', $time); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* 返回月份名 |
182
|
|
|
* |
183
|
|
|
* @param integer|null $time |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
1 |
|
public function monthName($time = null) |
188
|
|
|
{ |
189
|
1 |
|
null === $time && $time = time(); |
190
|
1 |
|
return date('F', $time); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* 是否是闰年 |
195
|
|
|
* |
196
|
|
|
* @param integer|null $time |
197
|
|
|
* |
198
|
|
|
* @return boolean |
199
|
|
|
*/ |
200
|
1 |
|
public function isLeapYear($time = null) |
201
|
|
|
{ |
202
|
1 |
|
null === $time && $time = time(); |
203
|
1 |
|
return (bool) date('L', $time); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* 返回该年的第几天 |
208
|
|
|
* |
209
|
|
|
* - 1 月 1 日为第一天 |
210
|
|
|
* |
211
|
|
|
* @param integer|null $time |
212
|
|
|
* |
213
|
|
|
* @return integer |
214
|
|
|
*/ |
215
|
1 |
|
public function yearPosition($time = null) |
216
|
|
|
{ |
217
|
1 |
|
null === $time && $time = time(); |
218
|
1 |
|
return (int) date('z', $time) + 1; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|