<?php

namespace app\utils;

class TimeUtil
{
    /**
     * 生成时间间隔数组
     */
    public static function generateTimeIntervals($startTime, $endTime, $intervalMinutes = 20): array
    {
        $intervals = [];
        $currentStart = strtotime($startTime);
        $endTimestamp = strtotime($endTime);

        while ($currentStart < $endTimestamp) {
            $currentEnd = $currentStart + ($intervalMinutes * 60);

            // 确保不超过结束时间
            if ($currentEnd > $endTimestamp) {
                $currentEnd = $endTimestamp;
            }

            $intervals[] = [
                'start' => date("Y-m-d H:i:00", $currentStart),
                'end' => date("Y-m-d H:i:00", $currentEnd)
            ];

            $currentStart = $currentEnd;

            // 如果已经到达结束时间，退出循环
            if ($currentStart >= $endTimestamp) {
                break;
            }
        }

        return $intervals;
    }

}