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