You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP日期数组:实现最长连续5天的新增日期限制逻辑

实现PHP日期数组的连续天数限制逻辑

嘿,我来帮你搞定这个日期添加的逻辑!核心目标就是保证新增日期后,任何连续的日期段长度都不超过5天,对吧?咱们一步步来实现:

核心思路

  1. 先把所有日期(现有数组+新增日期)转换成统一的可计算格式(比如DateTime对象),方便精准计算天数间隔。
  2. 找到新增日期和现有数组中日期的连续关联:也就是找出和新增日期前后相连的所有连续日期,计算这个连续区间的总天数。
  3. 如果这个连续区间的天数≤5,就允许添加;反之则拒绝。

代码实现示例

function canAddDate(array $existingDates, string $newDate): array {
    // 先验证新增日期格式是否合法
    $newDateTime = DateTime::createFromFormat('Y-m-d', $newDate);
    if (!$newDateTime) {
        return ['allowed' => false, 'message' => '日期格式错误,请使用Y-m-d格式'];
    }
    
    // 把现有数组转换成DateTime对象,过滤无效日期
    $dateObjects = array_map(function($date) {
        return DateTime::createFromFormat('Y-m-d', $date);
    }, $existingDates);
    $dateObjects = array_filter($dateObjects, fn($dt) => $dt !== false);
    
    // 临时合并数组并按日期排序
    $tempDates = [...$dateObjects, $newDateTime];
    usort($tempDates, fn($a, $b) => $a->getTimestamp() - $b->getTimestamp());
    
    // 定位新增日期在排序后数组中的位置
    $newDateIndex = array_search($newDateTime, $tempDates);
    if ($newDateIndex === false) {
        return ['allowed' => false, 'message' => '新增日期处理失败'];
    }
    
    // 向前遍历,找到连续日期的起始点
    $startIndex = $newDateIndex;
    while ($startIndex > 0) {
        $prevDate = $tempDates[$startIndex - 1];
        if ($tempDates[$startIndex]->diff($prevDate)->days === 1) {
            $startIndex--;
        } else {
            break;
        }
    }
    
    // 向后遍历,找到连续日期的结束点
    $endIndex = $newDateIndex;
    while ($endIndex < count($tempDates) - 1) {
        $nextDate = $tempDates[$endIndex + 1];
        if ($tempDates[$endIndex]->diff($nextDate)->days === 1) {
            $endIndex++;
        } else {
            break;
        }
    }
    
    // 计算连续区间的总天数
    $continuousDays = $endIndex - $startIndex + 1;
    
    if ($continuousDays <= 5) {
        // 允许添加,返回去重排序后的数组
        $updatedDates = [...$existingDates, $newDate];
        $updatedDates = array_unique($updatedDates);
        usort($updatedDates, fn($a, $b) => strtotime($a) - strtotime($b));
        return [
            'allowed' => true,
            'updatedDates' => $updatedDates,
            'message' => "日期添加成功,当前连续天数为{$continuousDays}"
        ];
    } else {
        return [
            'allowed' => false,
            'message' => "无法添加该日期,添加后会形成{$continuousDays}天的连续区间,超过限制的5天"
        ];
    }
}

// 测试你的案例
$existingDates = ["2018-02-05", "2018-02-06", "2018-02-07", "2018-02-08"];

// 测试添加2018-02-09
var_dump(canAddDate($existingDates, "2018-02-09"));
// 输出:allowed为true,连续天数5

// 测试添加2018-02-10
var_dump(canAddDate($existingDates, "2018-02-10"));
// 输出:allowed为false,连续天数6

// 测试添加2018-02-11
var_dump(canAddDate($existingDates, "2018-02-11"));
// 输出:allowed为true,连续天数1

代码说明

  • 先做了日期格式校验,避免无效日期干扰逻辑。
  • 使用DateTime的diff方法计算天数间隔,比直接用时间戳更可靠,能规避时区问题。
  • 通过前后遍历找到新增日期所在的连续区间,精准计算长度后判断是否符合限制。
  • 返回结果包含清晰的状态、更新后的数组(如果允许)和提示信息,方便你后续处理。

内容的提问来源于stack exchange,提问作者Hienz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:18:16