0

我正在尝试使用 PHP Carbon 库在提供的开始日期和结束日期之间填充日期数组。如果日期没有特定的顺序,这将是直截了当的。

这是场景:

我需要在每周四天填充日期数组。例如,这些日期的顺序必须像这样,星期二是开始日期的日期:

周二、周四、周六、周日所以我需要一种方法来获取开始日期并在迭代到下周之前添加 2 天然后 2 天然后 1 天。

这可以使用碳(CarbonPeriod/CarbonInterval)吗?

或者我的解决方案是否需要自定义实现?

4

2 回答 2

1

Carbon 完全能够做到这一点:

// $startDate as mentioned should be a valid Carbon date pointed to Tuesday

$dates = [];
for ($currentDate = $startDate; $currentDate <= $endDate; ) {
    $dates[] = $currentDate;
    $currentDate->addDays(2);
    $dates[] = $currentDate;
    $currentDate->addDays(2);
    $dates[] = $currentDate;
    $currentDate->addDay();
}
于 2021-12-08T17:10:02.110 回答
0

为什么不简单地DateTime::add循环使用(添加 2 天,然后 2 天,然后 1 天)?

这是文档

于 2021-12-08T15:13:27.397 回答