【PHP】针对时间段的处理

小破孩
2023-04-27 / 0 评论 / 407 阅读 / 正在检测是否收录...

    /**
     * 获取最近七天所有日期
     */
    public function getWeeks($time = '', $format='Y-m-d'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        for ($i=1; $i<=7; $i++){
            $date[$i] = date($format ,strtotime( '+' . $i-7 .' days', $time));
        }
        return $date;
    }
    /**
     * 获取起始日期中的所有日期
     * @param $start   开始时间   2022-09-22
     * @param $end     结束时间   2022-09-29
     * @return array
     */
    public function getBetweenTime($startDate, $endDate, $format = 'Y-m-d') {
        $dates = [];

        // 确保我们处理的是日期
        $start = date('Y-m-d', strtotime($startDate));
        $end = date('Y-m-d', strtotime($endDate));

        $current = strtotime($start);
        $last = strtotime($end);

        while ($current <= $last) {
            $dates[] = date($format, $current);
            $current = strtotime('+1 day', $current);
        }

        return $dates;
    }
    /**
     * 获取起始时间戳
     * @param int|string $type 类型 1本年 2本季度 3上月 4本月 5本周 6上周 7下周
     * @param bool $format 是否返回格式化后的时间(Y-m-d H:i:s),默认false返回时间戳
     * @return array 包含startTime(开始时间)和endTime(结束时间)的数组
     * @throws InvalidArgumentException
     */
    public function getStartEndtime($type = '', bool $format = false): array
    {
        // 验证类型参数
        $validTypes = [1, 2, 3, 4, 5, 6, 7];
        if ($type !== '' && !in_array($type, $validTypes)) {
            throw new Exception('无效的时间类型参数');
        }

        $now = time();
        $startTime = 0;
        $endTime = 0;

        switch ($type) {
            case 1:
                // 本年
                $startTime = strtotime("first day of January " . date('Y', $now) . " 00:00:00");
                $endTime = strtotime("last day of December " . date('Y', $now) . " 23:59:59");
                break;

            case 2:
                // 本季度
                $currentMonth = (int)date('n', $now);
                $currentYear = date('Y', $now);
                $season = ceil($currentMonth / 3); // 1-4
                $seasonStartMonth = ($season - 1) * 3 + 1;

                $startTime = mktime(0, 0, 0, $seasonStartMonth, 1, $currentYear);
                $seasonEndMonth = $season * 3;
                $seasonEndDay = date('t', mktime(0, 0, 0, $seasonEndMonth, 1, $currentYear));
                $endTime = mktime(23, 59, 59, $seasonEndMonth, $seasonEndDay, $currentYear);
                break;

            case 3:
                // 上月
                $startTime = strtotime("first day of last month 00:00:00");
                $endTime = strtotime("last day of last month 23:59:59");
                break;

            case 4:
                // 本月
                $startTime = strtotime("first day of this month 00:00:00");
                $endTime = strtotime("last day of this month 23:59:59");
                break;

            case 5:
                // 本周(周一到周日)
                $startTime = strtotime('this week Monday 00:00:00');
                $endTime = strtotime('this week Sunday 23:59:59');
                break;

            case 6:
                // 上周
                $startTime = strtotime('last week Monday 00:00:00');
                $endTime = strtotime('last week Sunday 23:59:59');
                break;

            case 7:
                // 下周
                $startTime = strtotime('next week Monday 00:00:00');
                $endTime = strtotime('next week Sunday 23:59:59');
                break;

            default:
                // 今天
                $startTime = strtotime('today 00:00:00');
                $endTime = strtotime('today 23:59:59');
                break;
        }

        // 格式化返回
        if ($format) {
            return [
                'statTime' => date('Y-m-d H:i:s', $startTime),
                'endTime' => date('Y-m-d H:i:s', $endTime)
            ];
        }

        return [
            'startTime' => $startTime,
            'endTime' => $endTime
        ];
    }

    /**
     * @Author:小破孩
     * @Email:3584685883@qq.com
     * @Time:2024/11/11 11:34
     * @return array
     * @Description:获取当前时间到一周前的时间
     */
    public function getTimeStamps() {
        $todayEnd = strtotime(date('Y-m-d 23:59:59'));
        $sevenDaysAgoStart = strtotime('-6 days 00:00:00');
        return [
            'start' => $sevenDaysAgoStart,
            'end' => $todayEnd
        ];
    }

   /**
     * @Author:小破孩
     * @Email:3584685883@qq.com
     * @Time:2024/12/7 11:05
     * @param $year
     * @param $month
     * @return array
     * @Description:获取指定月份的开始时间和结束时间
     */
    public function getMonthStartAndEndTime($year, $month) {
        $startTime = strtotime($year. '-'. $month. '-01 00:00:00');
        $endTime = strtotime($year. '-'. $month. '-'. date('t', strtotime($year. '-'. $month. '-01')). ' 23:59:59');
        return ['statTime'=>$startTime,'endTime'=>$endTime];
    }
0

评论 (0)

取消