|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.tintsoft.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd. |
|
5
|
|
|
* @license http://www.tintsoft.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yuncms\helpers; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use DateTime; |
|
12
|
|
|
use DateTimeZone; |
|
13
|
|
|
use yii\helpers\ArrayHelper; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DateHelper |
|
17
|
|
|
* |
|
18
|
|
|
* @author Tongle Xu <[email protected]> |
|
19
|
|
|
* @since 3.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class DateHelper |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* 获得时间戳 |
|
25
|
|
|
* |
|
26
|
|
|
* @param int $dateTime 默认为空,则以当前时间戳返回 |
|
27
|
|
|
* @return int |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function getTimestamp($dateTime = null): int |
|
30
|
|
|
{ |
|
31
|
|
|
return $dateTime ? is_numeric($dateTime) ? $dateTime : strtotime($dateTime) : time(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get all of the time zones with the offsets sorted by their offset |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getTimeZoneAll(): array |
|
39
|
|
|
{ |
|
40
|
|
|
$timezones = []; |
|
41
|
|
|
$identifiers = DateTimeZone::listIdentifiers(); |
|
42
|
|
|
foreach ($identifiers as $identifier) { |
|
43
|
|
|
$date = new DateTime("now", new DateTimeZone($identifier)); |
|
44
|
|
|
$offsetText = $date->format("P"); |
|
45
|
|
|
$offsetInHours = $date->getOffset() / 60 / 60; |
|
46
|
|
|
$timezones[] = [ |
|
47
|
|
|
"identifier" => $identifier, |
|
48
|
|
|
"name" => "(GMT{$offsetText}) $identifier", |
|
49
|
|
|
"offset" => $offsetInHours |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
ArrayHelper::multisort($timezones, "offset", SORT_ASC, SORT_NUMERIC); |
|
53
|
|
|
return $timezones; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* 获取星期 |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $week 星期,默认为当前时间获取 |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function getWeek($week = null): string |
|
63
|
|
|
{ |
|
64
|
|
|
$week = $week ? $week : date('w'); |
|
65
|
|
|
switch ($week) { |
|
66
|
|
|
case 'Sunday': |
|
67
|
|
|
return Yii::t('yuncms', 'Sunday'); |
|
68
|
|
|
break; |
|
|
|
|
|
|
69
|
|
|
case 'Monday': |
|
70
|
|
|
return Yii::t('yuncms', 'Monday'); |
|
71
|
|
|
break; |
|
72
|
|
|
case 'Tuesday': |
|
73
|
|
|
return Yii::t('yuncms', 'Tuesday'); |
|
74
|
|
|
break; |
|
75
|
|
|
case 'Wednesday': |
|
76
|
|
|
return Yii::t('yuncms', 'Wednesday'); |
|
77
|
|
|
break; |
|
78
|
|
|
case 'Thursday': |
|
79
|
|
|
return Yii::t('yuncms', 'Thursday'); |
|
80
|
|
|
break; |
|
81
|
|
|
case 'Friday': |
|
82
|
|
|
return Yii::t('yuncms', 'Friday'); |
|
83
|
|
|
break; |
|
84
|
|
|
case 'Saturday': |
|
85
|
|
|
return Yii::t('yuncms', 'Saturday'); |
|
86
|
|
|
break; |
|
87
|
|
|
} |
|
88
|
|
|
return 'N/A'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* 判断是否为闰年 |
|
93
|
|
|
* |
|
94
|
|
|
* @param int $year 年份,默认为当前年份 |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function isLeapYear($year = null): bool |
|
98
|
|
|
{ |
|
99
|
|
|
$year = $year ? $year : date('Y'); |
|
100
|
|
|
return ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* 获取一年中有多少天 |
|
105
|
|
|
* |
|
106
|
|
|
* @param int $year 年份,默认为当前年份 |
|
107
|
|
|
* @return int |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function getDaysInYear($year = null): int |
|
110
|
|
|
{ |
|
111
|
|
|
$year = $year ? $year : date('Y'); |
|
112
|
|
|
return self::isLeapYear($year) ? 366 : 365; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* 获取一天中的时段 |
|
117
|
|
|
* |
|
118
|
|
|
* @param int $hour 小时,默认为当前小时 |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function getPeriodOfTime($hour = null): string |
|
122
|
|
|
{ |
|
123
|
|
|
$hour = $hour ? $hour : date('G'); |
|
124
|
|
|
$period = null; |
|
125
|
|
|
if ($hour >= 0 && $hour < 6) { |
|
126
|
|
|
$period = '凌晨'; |
|
127
|
|
|
} elseif ($hour >= 6 && $hour < 8) { |
|
128
|
|
|
$period = '早晨'; |
|
129
|
|
|
} elseif ($hour >= 8 && $hour < 11) { |
|
130
|
|
|
$period = '上午'; |
|
131
|
|
|
} elseif ($hour >= 11 && $hour < 13) { |
|
132
|
|
|
$period = '中午'; |
|
133
|
|
|
} elseif ($hour >= 13 && $hour < 15) { |
|
134
|
|
|
$period = '响午'; |
|
135
|
|
|
} elseif ($hour >= 15 && $hour < 18) { |
|
136
|
|
|
$period = '下午'; |
|
137
|
|
|
} elseif ($hour >= 18 && $hour < 20) { |
|
138
|
|
|
$period = '傍晚'; |
|
139
|
|
|
} elseif ($hour >= 20 && $hour < 22) { |
|
140
|
|
|
$period = '晚上'; |
|
141
|
|
|
} elseif ($hour >= 22 && $hour <= 23) { |
|
142
|
|
|
$period = '深夜'; |
|
143
|
|
|
} |
|
144
|
|
|
return Yii::t('yuncms', $period); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* 日期数字转中文,适用于日、月、周 |
|
149
|
|
|
* |
|
150
|
|
|
* @param int $number 日期数字,默认为当前日期 |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public static function numberToChinese($number): string |
|
154
|
|
|
{ |
|
155
|
|
|
$chineseArr = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']; |
|
156
|
|
|
$chineseStr = null; |
|
157
|
|
|
if ($number < 10) { |
|
158
|
|
|
$chineseStr = $chineseArr [$number - 1]; |
|
159
|
|
|
} elseif ($number < 20) { |
|
160
|
|
|
$chineseStr = '十' . $chineseArr [$number - 11]; |
|
161
|
|
|
} elseif ($number < 30) { |
|
162
|
|
|
$chineseStr = '二十' . $chineseArr [$number - 21]; |
|
163
|
|
|
} else { |
|
164
|
|
|
$chineseStr = '三十' . $chineseArr [$number - 31]; |
|
165
|
|
|
} |
|
166
|
|
|
return $chineseStr; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* 年份数字转中文 |
|
171
|
|
|
* |
|
172
|
|
|
* @param int $year 年份数字,默认为当前年份 |
|
173
|
|
|
* @param bool $flag 是否增加公元 |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
public static function yearToChinese($year = null, $flag = false): string |
|
177
|
|
|
{ |
|
178
|
|
|
$year = $year ? intval($year) : date('Y'); |
|
179
|
|
|
$data = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; |
|
180
|
|
|
$chinese_str = null; |
|
181
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
182
|
|
|
$chinese_str .= $data [substr($year, $i, 1)]; |
|
183
|
|
|
} |
|
184
|
|
|
return $flag ? '公元' . $chinese_str : $chinese_str; |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* 获取日期所属的星座、干支、生肖 |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $type 获取信息类型(SX:生肖、GZ:干支、XZ:星座) |
|
191
|
|
|
* @param int $date |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
|
|
public static function dateInfo($type, $date = null): string |
|
195
|
|
|
{ |
|
196
|
|
|
$year = date('Y', $date); |
|
197
|
|
|
$month = date('m', $date); |
|
198
|
|
|
$day = date('d', $date); |
|
199
|
|
|
$result = null; |
|
200
|
|
|
switch ($type) { |
|
201
|
|
|
case 'SX' : |
|
202
|
|
|
$data = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪']; |
|
203
|
|
|
$result = $data [($year - 4) % 12]; |
|
204
|
|
|
break; |
|
205
|
|
|
case 'GZ' : |
|
206
|
|
|
$data = [['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'], ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥']]; |
|
207
|
|
|
$num = $year - 1900 + 36; |
|
208
|
|
|
$result = $data [0] [$num % 10] . $data [1] [$num % 12]; |
|
209
|
|
|
break; |
|
210
|
|
|
case 'XZ' : |
|
211
|
|
|
$data = ['摩羯', '宝瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手']; |
|
212
|
|
|
$zone = [1222, 122, 222, 321, 421, 522, 622, 722, 822, 922, 1022, 1122, 1222]; |
|
213
|
|
|
if ((100 * $month + $day) >= $zone [0] || (100 * $month + $day) < $zone [1]) { |
|
214
|
|
|
$i = 0; |
|
215
|
|
|
} else { |
|
216
|
|
|
for ($i = 1; $i < 12; $i++) { |
|
217
|
|
|
if ((100 * $month + $day) >= $zone [$i] && (100 * $month + $day) < $zone [$i + 1]) break; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
$result = $data [$i] . '座'; |
|
221
|
|
|
break; |
|
222
|
|
|
} |
|
223
|
|
|
return $result; |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* 获取两个日期的差 |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $interval 日期差的间隔类型,(Y:年、M:月、W:星期、D:日期、H:时、N:分、S:秒) |
|
230
|
|
|
* @param int $startDateTime 开始日期 |
|
231
|
|
|
* @param int $endDateTime 结束日期 |
|
232
|
|
|
* @return int |
|
233
|
|
|
*/ |
|
234
|
|
|
public static function dateDiff($interval, $startDateTime, $endDateTime): int |
|
235
|
|
|
{ |
|
236
|
|
|
$diff = static::getTimestamp($endDateTime) - static::getTimestamp($startDateTime); |
|
237
|
|
|
switch ($interval) { |
|
238
|
|
|
case 'Y' : // 年 |
|
239
|
|
|
$result = bcdiv($diff, 60 * 60 * 24 * 365); |
|
240
|
|
|
break; |
|
241
|
|
|
case 'M' : // 月 |
|
242
|
|
|
$result = bcdiv($diff, 60 * 60 * 24 * 30); |
|
243
|
|
|
break; |
|
244
|
|
|
case 'W' : // 星期 |
|
245
|
|
|
$result = bcdiv($diff, 60 * 60 * 24 * 7); |
|
246
|
|
|
break; |
|
247
|
|
|
case 'D' : // 日 |
|
248
|
|
|
$result = bcdiv($diff, 60 * 60 * 24); |
|
249
|
|
|
break; |
|
250
|
|
|
case 'H' : // 时 |
|
251
|
|
|
$result = bcdiv($diff, 60 * 60); |
|
252
|
|
|
break; |
|
253
|
|
|
case 'N' : // 分 |
|
254
|
|
|
$result = bcdiv($diff, 60); |
|
255
|
|
|
break; |
|
256
|
|
|
case 'S' : // 秒 |
|
257
|
|
|
default : |
|
258
|
|
|
$result = $diff; |
|
259
|
|
|
break; |
|
260
|
|
|
} |
|
261
|
|
|
return $result; |
|
|
|
|
|
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* 返回指定日期在一段时间间隔时间后的日期 |
|
266
|
|
|
* |
|
267
|
|
|
* @param string $interval 时间间隔类型,(Y:年、Q:季度、M:月、W:星期、D:日期、H:时、N:分、S:秒) |
|
268
|
|
|
* @param int $value 时间间隔数值,数值为正数获取未来的时间,数值为负数获取过去的时间 |
|
269
|
|
|
* @param string $dateTime 日期 |
|
270
|
|
|
* @param string $format 返回的日期转换格式 |
|
271
|
|
|
* @return string 返回追加后的日期 |
|
272
|
|
|
*/ |
|
273
|
|
|
public static function dateAdd($interval, $value, $dateTime = null, $format = null): string |
|
274
|
|
|
{ |
|
275
|
|
|
$dateTime = $dateTime ? $dateTime : date('Y-m-d H:i:s'); |
|
276
|
|
|
$date = getdate(self::getTimestamp($dateTime)); |
|
|
|
|
|
|
277
|
|
|
switch ($interval) { |
|
278
|
|
|
case 'Y' : // 年 |
|
279
|
|
|
$date ['year'] += $value; |
|
280
|
|
|
break; |
|
281
|
|
|
case 'Q' : // 季度 |
|
282
|
|
|
$date ['mon'] += ($value * 3); |
|
283
|
|
|
break; |
|
284
|
|
|
case 'M' : // 月 |
|
285
|
|
|
$date ['mon'] += $value; |
|
286
|
|
|
break; |
|
287
|
|
|
case 'W' : // 星期 |
|
288
|
|
|
$date ['mday'] += ($value * 7); |
|
289
|
|
|
break; |
|
290
|
|
|
case 'D' : // 日 |
|
291
|
|
|
$date ['mday'] += $value; |
|
292
|
|
|
break; |
|
293
|
|
|
case 'H' : // 时 |
|
294
|
|
|
$date ['hours'] += $value; |
|
295
|
|
|
break; |
|
296
|
|
|
case 'N' : // 分 |
|
297
|
|
|
$date ['minutes'] += $value; |
|
298
|
|
|
break; |
|
299
|
|
|
case 'S' : // 秒 |
|
300
|
|
|
default : |
|
301
|
|
|
$date ['seconds'] += $value; |
|
302
|
|
|
break; |
|
303
|
|
|
} |
|
304
|
|
|
return date($format, mktime($date ['hours'], $date ['minutes'], $date ['seconds'], $date ['mon'], $date ['mday'], $date ['year'])); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* 根据年份获取每个月的天数 |
|
309
|
|
|
* |
|
310
|
|
|
* @param int $year 年份 |
|
311
|
|
|
* @return array 月份天数数组 |
|
312
|
|
|
*/ |
|
313
|
|
|
public static function getDaysByMonthsOfYear($year = null): array |
|
314
|
|
|
{ |
|
315
|
|
|
$year = $year ? $year : date('Y'); |
|
316
|
|
|
$months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
|
317
|
|
|
if (self::isLeapYear($year)) $months [1] = 29; |
|
|
|
|
|
|
318
|
|
|
return $months; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* 返回某年的某个月有多少天 |
|
323
|
|
|
* |
|
324
|
|
|
* @param int $month 月份 |
|
325
|
|
|
* @param int $year 年份 |
|
326
|
|
|
* @return int 月份天数 |
|
327
|
|
|
*/ |
|
328
|
|
|
public static function getDaysByMonth($month, $year): int |
|
329
|
|
|
{ |
|
330
|
|
|
$months = self::getDaysByMonthsOfYear($year); |
|
331
|
|
|
$value = $months [$month - 1]; |
|
332
|
|
|
return !$value ? 0 : $value; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* 获取两个日期之间范围 |
|
337
|
|
|
* |
|
338
|
|
|
* @param string $startDateTime |
|
339
|
|
|
* @param string $endDateTime |
|
340
|
|
|
* @param bool $sort |
|
341
|
|
|
* @param string $format |
|
342
|
|
|
* @return array 返回日期数组 |
|
343
|
|
|
*/ |
|
344
|
|
|
public static function getDayRangeInBetweenDate($startDateTime, $endDateTime, $sort = false, $format = 'Y-m-d'): array |
|
345
|
|
|
{ |
|
346
|
|
|
$startDateTime = self::getTimestamp($startDateTime); |
|
|
|
|
|
|
347
|
|
|
$endDateTime = self::getTimestamp($endDateTime); |
|
348
|
|
|
$num = ($endDateTime - $startDateTime) / 86400; |
|
349
|
|
|
$arr = []; |
|
350
|
|
|
for ($i = 0; $i <= $num; $i++) { |
|
351
|
|
|
$arr [] = date($format, $startDateTime + 86400 * $i); |
|
352
|
|
|
} |
|
353
|
|
|
return $sort ? array_reverse($arr) : $arr; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* 获取年份的第一天 |
|
358
|
|
|
* |
|
359
|
|
|
* @param int $year 年份 |
|
360
|
|
|
* @param string $format 返回的日期格式 |
|
361
|
|
|
* @return string 返回的日期 |
|
362
|
|
|
*/ |
|
363
|
|
|
public static function firstDayOfYear($year = null, $format = 'Y-m-d'): string |
|
364
|
|
|
{ |
|
365
|
|
|
$year = $year ? $year : date('Y'); |
|
366
|
|
|
return date($format, mktime(0, 0, 0, 1, 1, $year)); |
|
|
|
|
|
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* 获取年份最后一天 |
|
371
|
|
|
* |
|
372
|
|
|
* @param int $year 年份 |
|
373
|
|
|
* @param string $format 返回的日期格式 |
|
374
|
|
|
* @return string 返回的日期 |
|
375
|
|
|
*/ |
|
376
|
|
|
public static function lastDayOfYear($year = null, $format = 'Y-m-d'): string |
|
377
|
|
|
{ |
|
378
|
|
|
$year = $year ? $year : date('Y'); |
|
379
|
|
|
return date($format, mktime(0, 0, 0, 1, 0, $year + 1)); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* 获取月份的第一天 |
|
384
|
|
|
* |
|
385
|
|
|
* @param int $month 月份 |
|
386
|
|
|
* @param int $year 年份 |
|
387
|
|
|
* @param string $format 返回的日期格式 |
|
388
|
|
|
* @return string 返回的日期 |
|
389
|
|
|
*/ |
|
390
|
|
|
public static function firstDayOfMonth($month = null, $year = null, $format = 'Y-m-d'): string |
|
391
|
|
|
{ |
|
392
|
|
|
$year = $year ? $year : date('Y'); |
|
393
|
|
|
$month = $month ? $month : date('m'); |
|
394
|
|
|
return date($format, mktime(0, 0, 0, $month, 1, $year)); |
|
|
|
|
|
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* 获取月份最后一天 |
|
399
|
|
|
* |
|
400
|
|
|
* @param int $month 月份 |
|
401
|
|
|
* @param int $year 年份 |
|
402
|
|
|
* @param string $format 返回的日期格式 |
|
403
|
|
|
* @return string 返回的日期 |
|
404
|
|
|
*/ |
|
405
|
|
|
public static function lastDayOfMonth($month = null, $year = null, $format = 'Y-m-d'): string |
|
406
|
|
|
{ |
|
407
|
|
|
$year = $year ? $year : date('Y'); |
|
408
|
|
|
$month = $month ? $month : date('m'); |
|
409
|
|
|
return date($format, mktime(0, 0, 0, $month + 1, 0, $year)); |
|
|
|
|
|
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* 获取今天开始时间戳 |
|
414
|
|
|
* @return int |
|
415
|
|
|
*/ |
|
416
|
|
|
public static function todayFirstSecond(): int |
|
417
|
|
|
{ |
|
418
|
|
|
return mktime(0, 0, 0, date("m", time()), date("d", time()), date("Y", time())); |
|
|
|
|
|
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* 获取今天结束时间戳 |
|
423
|
|
|
* @return int |
|
424
|
|
|
*/ |
|
425
|
|
|
public static function todayLastSecond(): int |
|
426
|
|
|
{ |
|
427
|
|
|
return mktime(23, 59, 59, date("m", time()), date("d", time()), date("Y", time())); |
|
|
|
|
|
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* 获取本周开始时间戳 |
|
432
|
|
|
* @return int |
|
433
|
|
|
*/ |
|
434
|
|
|
public static function weekFirstSecond(): int |
|
435
|
|
|
{ |
|
436
|
|
|
return strtotime(date('Y-m-d', time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* 获取本周结束时间戳 |
|
441
|
|
|
* |
|
442
|
|
|
* @return int |
|
443
|
|
|
*/ |
|
444
|
|
|
public static function weekLastSecond(): int |
|
445
|
|
|
{ |
|
446
|
|
|
return strtotime(date('Y-m-d', time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600) . ' 23:59:59'); |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* 获取上周开始时间戳 |
|
451
|
|
|
* @return int |
|
452
|
|
|
*/ |
|
453
|
|
|
public static function lastWeekFirstSecond(): int |
|
454
|
|
|
{ |
|
455
|
|
|
return strtotime('-1 monday', time()); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* 获取上周结束时间戳 |
|
460
|
|
|
* @return false|int |
|
461
|
|
|
*/ |
|
462
|
|
|
public static function lastWeekLastSecond() |
|
463
|
|
|
{ |
|
464
|
|
|
return strtotime('-1 sunday', time()) + 86399; |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
/** |
|
468
|
|
|
* 获取上月开始时间戳 |
|
469
|
|
|
* @return int |
|
470
|
|
|
*/ |
|
471
|
|
|
public static function lastMonthFirstSecond(): int |
|
472
|
|
|
{ |
|
473
|
|
|
return strtotime(date('Y-m', strtotime('-1 month', time())) . '-01 00:00:00'); |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
|
|
* 获取上月结束时间戳 |
|
478
|
|
|
* @return mixed |
|
479
|
|
|
*/ |
|
480
|
|
|
public static function lastMonthLastSecond(): int |
|
481
|
|
|
{ |
|
482
|
|
|
return strtotime(date('Y-m', strtotime('-1 month', time())) . '-' . date('t', strtotime('-1 month', time())) . ' 23:59:59'); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* 获取本月1日0点时间戳 |
|
487
|
|
|
* |
|
488
|
|
|
* @return int |
|
489
|
|
|
*/ |
|
490
|
|
|
public static function monthFirstSecond(): int |
|
491
|
|
|
{ |
|
492
|
|
|
return strtotime(date('Y-m', time()) . '-01 00:00:00'); |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
/** |
|
496
|
|
|
* 获取本月最后一日结束时间戳 |
|
497
|
|
|
* @return int |
|
498
|
|
|
*/ |
|
499
|
|
|
public static function monthLastSecond(): int |
|
500
|
|
|
{ |
|
501
|
|
|
return strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 23:59:59'); |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* 获取下月1日开始时间戳 |
|
506
|
|
|
* |
|
507
|
|
|
* @return int |
|
508
|
|
|
*/ |
|
509
|
|
|
public static function nextMonthFirstSecond(): int |
|
510
|
|
|
{ |
|
511
|
|
|
return static::monthLastSecond() + 86400; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* 获取下月最后一日结束时间戳 |
|
516
|
|
|
* |
|
517
|
|
|
* @return int |
|
518
|
|
|
*/ |
|
519
|
|
|
public static function nextMonthLastSecond(): int |
|
520
|
|
|
{ |
|
521
|
|
|
return strtotime(date('Y-m', static::nextMonthFirstSecond()) . '-' . date('t', static::nextMonthFirstSecond()) . ' 23:59:59'); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
/** |
|
525
|
|
|
* 获取上季度开始 |
|
526
|
|
|
* |
|
527
|
|
|
* @return int |
|
528
|
|
|
*/ |
|
529
|
|
|
public static function lastQuarterFirstSecond(): int |
|
530
|
|
|
{ |
|
531
|
|
|
$season = ceil((date('n')) / 3) - 1; |
|
532
|
|
|
return mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')); |
|
|
|
|
|
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* 获取上季度结束 |
|
537
|
|
|
* |
|
538
|
|
|
* @return int |
|
539
|
|
|
*/ |
|
540
|
|
|
public static function lastQuarterLastSecond(): int |
|
541
|
|
|
{ |
|
542
|
|
|
$season = ceil((date('n')) / 3) - 1; |
|
543
|
|
|
return mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')); |
|
|
|
|
|
|
544
|
|
|
} |
|
545
|
|
|
|
|
546
|
|
|
/** |
|
547
|
|
|
* 获取本季度开始 |
|
548
|
|
|
* |
|
549
|
|
|
* @return int |
|
550
|
|
|
*/ |
|
551
|
|
|
public static function QuarterFirstSecond(): int |
|
552
|
|
|
{ |
|
553
|
|
|
$season = ceil((date('n')) / 3); |
|
554
|
|
|
return mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')); |
|
|
|
|
|
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* 获取本季度结束 |
|
559
|
|
|
* @return int |
|
560
|
|
|
*/ |
|
561
|
|
|
public static function QuarterLastSecond(): int |
|
562
|
|
|
{ |
|
563
|
|
|
$season = ceil((date('n')) / 3); |
|
564
|
|
|
return mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')); |
|
|
|
|
|
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
/** |
|
568
|
|
|
* 获取本年开始时间戳 |
|
569
|
|
|
* @return int |
|
570
|
|
|
*/ |
|
571
|
|
|
public static function yearFirstSecond(): int |
|
572
|
|
|
{ |
|
573
|
|
|
return strtotime(date('Y') . '-01-01 00:00:00'); |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
/** |
|
577
|
|
|
* 获取本年结束时间戳 |
|
578
|
|
|
* @return int |
|
579
|
|
|
*/ |
|
580
|
|
|
public static function yearLastSecond(): int |
|
581
|
|
|
{ |
|
582
|
|
|
return strtotime(date('Y') . '-12-31 23:59:59'); |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
|
|
586
|
|
|
} |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: