根据开始时间 结束时间返回 日期列表

/**
 * startDay 开始时间 2022-10-01
 * endDay 结束时间 2022-10-31
 * return 以具体日期为下标的数据 可根据需求更改代码
 */
function getDayList($startDay, $endDay)
{

    $begin = new \DateTime($startDay);
    $end = new \DateTime($endDay);

    $interval = \DateInterval::createFromDateString('1 day'); //间隔几天
    $period = new \DatePeriod($begin, $interval, $end);

    $dayList = [];
    foreach ($period as $dt) {
        $temp = $dt->format("Y-m-d");
        $dayList[$temp] = [];
    }

    // 若是连续天数 把最后一天也加上
    $dayList[$endDay] = []; 

    return $dayList;
}

同上

// 可返回周期内的周几的日期
function getDayList($start, $end)
{
    $dayList = [];
    $startTime = strtotime($start);
    $endTime = strtotime($end);
    for ($startTime; $startTime <= $endTime; $startTime += 86400) {
        $dayList[] =  date('Y-m-d', $startTime);;
    }

    return $dayList;

}

根据日期区间 获取连续年月份

    /**
     * 获取时间区间连续年月份
     * 2022-10-01
     * 2023-10-23
     */ 
    function getMonthList($start, $end)
    {
        $startTime = strtotime($start);
        $endTime = strtotime($end);
        $month = [];
        while ($startTime <= $endTime) {
            $newMonth = $month ? date('Y-m-01',  strtotime("+1 month", $startTime)) : date('Y-m-01',$startTime);
            $month[] = $newMonth;
            $startTime = strtotime($newMonth);
        }

        array_pop($month);
        return $month;
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!