闫增涛
2025-04-14 28de42c3542059013d31e465e49de854dc789f07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
declare (strict_types = 1);
 
namespace dateset;
 
/**
 * 日期时间处理类
 */
class Dateset
{
    const YEAR = 31536000;
    const MONTH = 2592000;
    const WEEK = 604800;
    const DAY = 86400;
    const HOUR = 3600;
    const MINUTE = 60;
 
    /**
     * 间隔时间段格式化
     * @param int $time 时间戳
     * @param string $format 格式 【d:显示到天 i显示到分钟 s显示到秒】
     * @return string
     */
    function time_trans($time, $format = 'd')
    {
        $now = time();
        $diff = $now - $time;
        if ($diff < self::MINUTE) {
            return '1分钟前';
        } else if ($diff < self::HOUR) {
            return floor($diff / self::MINUTE) . '分钟前';
        } else if ($diff < self::DAY) {
            return floor($diff / self::HOUR) . '小时前';
        }
        $yes_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-1 days'))); //昨天开始时间
        $yes_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-1 days'))); //昨天结束时间
        $two_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-2 days'))); //2天前结束时间
        $three_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-3 days'))); //3天前结束时间
        $four_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-4 days'))); //4天前结束时间
        $five_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-5 days'))); //5天前结束时间
        $six_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-6 days'))); //6天前结束时间
        $seven_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-7 days'))); //7天前结束时间
 
        if ($time > $yes_start_time && $time < $yes_end_time) {
            return '昨天';
        }
 
        if ($time > $yes_start_time && $time < $two_end_time) {
            return '1天前';
        }
 
        if ($time > $yes_start_time && $time < $three_end_time) {
            return '2天前';
        }
 
        if ($time > $yes_start_time && $time < $four_end_time) {
            return '3天前';
        }
 
        if ($time > $yes_start_time && $time < $five_end_time) {
            return '4天前';
        }
 
        if ($time > $yes_start_time && $time < $six_end_time) {
            return '5天前';
        }
 
        if ($time > $yes_start_time && $time < $seven_end_time) {
            return '6天前';
        }
 
        switch ($format) {
            case 'd':
                $show_time = date('Y-m-d', $time);
                break;
            case 'i':
                $show_time = date('Y-m-d H:i', $time);
                break;
            case 's':
                $show_time = date('Y-m-d H:i:s', $time);
                break;
        }
        return $show_time;
    }
 
 
    /**
     * 计算两个时间戳之间相差的时间
     *
     * $differ = self::differ(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
     * $differ = self::differ(60, 182, 'minutes'); // 2
     *
     * @param int    $remote timestamp to find the span of
     * @param int    $local  timestamp to use as the baseline
     * @param string $output formatting string
     * @return  string   when only a single output is requested
     * @return  array    associative list of all outputs requested
     * @from https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php
     */
    public static function differ($remote, $local = null, $output = 'years,months,weeks,days,hours,minutes,seconds')
    {
        // Normalize output
        $output = trim(strtolower((string)$output));
        if (!$output) {
            // Invalid output
            return false;
        }
        // Array with the output formats
        $output = preg_split('/[^a-z]+/', $output);
        // Convert the list of outputs to an associative array
        $output = array_combine($output, array_fill(0, count($output), 0));
        // Make the output values into keys
        extract(array_flip($output), EXTR_SKIP);
        if ($local === null) {
            // Calculate the span from the current time
            $local = time();
        }
        // Calculate timespan (seconds)
        $timespan = abs($remote - $local);
        if (isset($output['years'])) {
            $timespan -= self::YEAR * ($output['years'] = (int)floor($timespan / self::YEAR));
        }
        if (isset($output['months'])) {
            $timespan -= self::MONTH * ($output['months'] = (int)floor($timespan / self::MONTH));
        }
        if (isset($output['weeks'])) {
            $timespan -= self::WEEK * ($output['weeks'] = (int)floor($timespan / self::WEEK));
        }
        if (isset($output['days'])) {
            $timespan -= self::DAY * ($output['days'] = (int)floor($timespan / self::DAY));
        }
        if (isset($output['hours'])) {
            $timespan -= self::HOUR * ($output['hours'] = (int)floor($timespan / self::HOUR));
        }
        if (isset($output['minutes'])) {
            $timespan -= self::MINUTE * ($output['minutes'] = (int)floor($timespan / self::MINUTE));
        }
        // Seconds ago, 1
        if (isset($output['seconds'])) {
            $output['seconds'] = $timespan;
        }
        if (count($output) === 1) {
            // Only a single output was requested, return it
            return array_pop($output);
        }
        // Return array
        return $output;
    }
 
    /**
     * 获取指定年月拥有的天数
     * @param int $month
     * @param int $year
     * @return false|int|string
     */
    public static function days_in_month($month, $year)
    {
        if (function_exists("cal_days_in_month")) {
            return cal_days_in_month(CAL_GREGORIAN, $month, $year);
        } else {
            return date('t', mktime(0, 0, 0, $month, 1, $year));
        }
    }
    
 
    /**
     * 将秒数转换为时间 (小时、分、秒)
     * @param
     */
    function getTimeBySec($time,$second=true)
    {
        if (is_numeric($time)) {
            $value = array(
                "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            $t='';
            if ($time >= 3600) {
                $value["hours"] = floor($time / 3600);
                $time = ($time % 3600);
                $t .= $value["hours"] . "小时";
            }
            if ($time >= 60) {
                $value["minutes"] = floor($time / 60);
                $time = ($time % 60);
                $t .= $value["minutes"] . "分钟";
            }
            if ($time > 0 && $time < 60 && $second==true) {
                $value["seconds"] = floor($time);
                $t .= $value["seconds"] . "秒";
            }
            return $t;
        } else {
            return (bool)FALSE;
        }
    }
 
    /**
     * 将秒数转换为时间 (年、天、小时、分、秒)
     * @param
     */
    function getDateBySec($time,$second=false)
    {
        if (is_numeric($time)) {
            $value = array(
                "years" => 0, "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            $t='';
            if ($time >= 31556926) {
                $value["years"] = floor($time / 31556926);
                $time = ($time % 31556926);
                $t .= $value["years"] . "年";
            }
            if ($time >= 86400) {
                $value["days"] = floor($time / 86400);
                $time = ($time % 86400);
                $t .= $value["days"] . "天";
            }
            if ($time >= 3600) {
                $value["hours"] = floor($time / 3600);
                $time = ($time % 3600);
                $t .= $value["hours"] . "小时";
            }
            if ($time >= 60) {
                $value["minutes"] = floor($time / 60);
                $time = ($time % 60);
                $t .= $value["minutes"] . "分钟";
            }
            if ($time < 60 && $second==true) {
                $value["seconds"] = floor($time);
                $t .= $value["seconds"] . "秒";
            }
            return $t;
        } else {
            return (bool)FALSE;
        }
    }
 
    /*
     *根据年月计算有几天
     */
    function getmonthByYM($param)
    {
        $month = $param['month'] ? $param['month'] : date('m', time());
        $year = $param['year'] ? $param['year'] : date('Y', time());
        if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) {
            $days = '31';
        } elseif ($month == 2) {
            if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
                //判断是否是闰年  
                $days = '29';
            } else {
                $days = '28';
            }
        } else {
            $days = '30';
        }
        return $days;
    }
 
    /**
     * 根据时间戳计算当月天数
     * @param
     */
    function getmonthdays($time)
    {
        $month = date('m', $time);
        $year = date('Y', $time);
        if (in_array($month, array('1', '3', '5', '7', '8', '01', '03', '05', '07', '08', '10', '12'))) {
            $days = '31';
        } elseif ($month == 2) {
            if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 !== 0)) {
                //判断是否是闰年  
                $days = '29';
            } else {
                $days = '28';
            }
        } else {
            $days = '30';
        }
        return $days;
    }
 
    /**
     * 生成从开始时间到结束时间的日期数组
     * @param type,默认时间戳格式
     * @param type = 1 时,date格式
     * @param type = 2 时,获取每日开始、结束时间
     */
    function dateList($start, $end, $type = 0)
    {
        if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
        $i = 0;
        //从开始日期到结束日期的每日时间戳数组
        $d = array();
        if ($type == 1) {
            while ($start <= $end) {
                $d[$i] = date('Y-m-d', $start);
                $start = $start + 86400;
                $i++;
            }
        } else {
            while ($start <= $end) {
                $d[$i] = $start;
                $start = $start + 86400;
                $i++;
            }
        }
        if ($type == 2) {
            $list = array();
            foreach ($d as $k => $v) {
                $list[$k] = $this->getDateRange($v);
            }
            return $list;
        } else {
            return $d;
        }
    }
 
    /**
     * 获取指定日期开始时间与结束时间
     */
    function getDateRange($timestamp)
    {
        $ret = array();
        $ret['sdate'] = strtotime(date('Y-m-d', $timestamp));
        $ret['edate'] = strtotime(date('Y-m-d', $timestamp)) + 86400;
        return $ret;
    }
 
    /**
     * 生成从开始月份到结束月份的月份数组
     * @param int $start 开始时间戳
     * @param int $end 结束时间戳
     */
    function monthList($start, $end)
    {
        if (!is_numeric($start) || !is_numeric($end) || ($end <= $start)) return '';
        $start = date('Y-m', $start);
        $end = date('Y-m', $end);
        //转为时间戳
        $start = strtotime($start . '-01');
        $end = strtotime($end . '-01');
        $i = 0;
        $d = array();
        while ($start <= $end) {
            //这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数
            $d[$i] = $start;
            $start += strtotime('+1 month', $start) - $start;
            $i++;
        }
        return $d;
    }
 
    /**
     * 等于(时间段)数据处理
     *
     * @param $type
     * @return array
     * @since 2021-06-11
     * @author fanqi
     */
    function advancedDate($type)
    {
        // 本年度
        if ($type == 'year') {
            $arrTime = DataTime::year();
            $start_time = date('Y-m-d 00:00:00', $arrTime[0]);
            $end_time = date('Y-m-d 23:59:59', $arrTime[1]);
        }
 
        // 上一年度
        if ($type == 'lastYear') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 year'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 year'));
        }
 
        // 下一年度
        if ($type == 'nextYear') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 year'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 year'));
        }
 
        // 上半年
        if ($type == 'firstHalfYear') {
            $start_time = date('Y-01-01 00:00:00');
            $end_time = date('Y-06-30 23:59:59');
        }
 
        // 下半年
        if ($type == 'nextHalfYear') {
            $start_time = date('Y-07-01 00:00:00');
            $end_time = date('Y-12-31 23:59:59');
        }
 
        // 本季度
        if ($type == 'quarter') {
            $season = ceil((date('n')) / 3);
            $start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
            $end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
        }
 
        // 上一季度
        if ($type == 'lastQuarter') {
            $season = ceil((date('n')) / 3) - 1;
            $start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y')));
            $end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
        }
 
        // 下一季度
        if ($type == 'nextQuarter') {
            $season = ceil((date('n')) / 3);
            $start_time = date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 + 1, 1, date('Y')));
            $end_time = date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3 + 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')));
        }
 
        // 本月
        if ($type == 'month') {
            $start_time = date('Y-m-01 00:00:00');
            $end_time = date('Y-m-31 23:59:59');
        }
 
        // 上月
        if ($type == 'lastMonth') {
            $start_time = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '-1 month'));
            $end_time = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '-1 month'));
        }
 
        // 下月
        if ($type == 'nextMonth') {
            $start_time = date('Y-m-01 00:00:00', strtotime(date('Y-m-d') . '+1 month'));
            $end_time = date('Y-m-31 23:59:59', strtotime(date('Y-m-d') . '+1 month'));
        }
 
        // 本周
        if ($type == 'week') {
            $start_time = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')));
            $end_time = date('Y-m-d 23:59:59', mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y')));
        }
 
        // 上周
        if ($type == 'lastWeek') {
            $date = date("Y-m-d");
            $w = date("w", strtotime($date));
            $d = $w ? $w - 1 : 6;
            $start = date("Y-m-d", strtotime($date . " - " . $d . " days"));
            $start_time = date('Y-m-d', strtotime($start . " - 7 days"));
            $end_time = date('Y-m-d', strtotime($start . " - 1 days"));
        }
 
        // 下周
        if ($type == 'nextWeek') {
            $date = date("Y-m-d");
            $w = date("w", strtotime($date));
            $d = $w ? $w - 1 : 6;
            $start = date("Y-m-d", strtotime($date . " - " . $d . " days"));
            $start_time = date('Y-m-d', strtotime($start . " + 7 days"));
            $end_time = date('Y-m-d', strtotime($start . " + 13 days"));
        }
 
        // 今天
        if ($type == 'today') {
            $start_time = date('Y-m-d 00:00:00');
            $end_time = date('Y-m-d 23:59:59');
        }
 
        // 昨天
        if ($type == 'yesterday') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-1 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
        }
 
        // 明天
        if ($type == 'tomorrow') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+1 day'));
        }
 
        // 过去3天
        if ($type == 'previous3day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-3 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
        }
 
        // 过去5天
        if ($type == 'previous5day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-5 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
        }
 
        // 过去7天
        if ($type == 'previous7day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-7 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
        }
        // 过去10天
        if ($type == 'previous10day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-10 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
        }
        // 过去30天
        if ($type == 'previous30day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '-30 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '-1 day'));
        }
        // 未来3天
        if ($type == 'future3day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+3 day'));
        }
        // 未来5天
        if ($type == 'future5day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+5 day'));
        }
        // 未来7天
        if ($type == 'future7day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+7 day'));
        }
        // 未来10天
        if ($type == 'future10day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+10 day'));
        }
        // 未来30天
        if ($type == 'future30day') {
            $start_time = date('Y-m-d 00:00:00', strtotime(date('Y-m-d') . '+1 day'));
            $end_time = date('Y-m-d 23:59:59', strtotime(date('Y-m-d') . '+30 day'));
        }
        return [$start_time,$end_time];
    }
 
    /**
     * 根据时间戳获取星期几
     * @param $time 要转换的时间戳
     */
    function getTimeWeek($time, $i = 0)
    {
        $weekarray = array("日", "一", "二", "三", "四", "五", "六");
        $oneD = 24 * 60 * 60;
        return "星期" . $weekarray[date("w", $time + $oneD * $i)];
    }
}