gpt4 book ai didi

php - 如何通过碳获取日期范围内的所有星期五

转载 作者:行者123 更新时间:2023-12-03 01:00:37 26 4
gpt4 key购买 nike

我使用此函数来获取两个日期之间的所有星期五:

public function getFridaysInRange($dateFromString, $dateToString)
{
$dateFrom = new \DateTime($dateFromString);
$dateTo = new \DateTime($dateToString);
$dates = [];

if ($dateFrom > $dateTo) {
return $dates;
}

if (1 != $dateFrom->format('N')) {
$dateFrom->modify('next friday');
}

while ($dateFrom <= $dateTo) {
$dates[] = $dateFrom->format('Y-m-d');
$dateFrom->modify('+1 week');
}

return $dates;
}

$this->getFridaysInRange('2017-01-01','2017-01-30');

结果:

array:4 [▼
0 => "2017-01-06"
1 => "2017-01-13"
2 => "2017-01-20"
3 => "2017-01-27"
]

carbon中有像上面这样的函数吗?

最佳答案

您可以像这样使用 Carbon 的所有功能:

$fridays = [];
$startDate = Carbon::parse($fromDate)->next(Carbon::FRIDAY); // Get the first friday.
$endDate = Carbon::parse($toDate);

for ($date = $startDate; $date->lte($endDate); $date->addWeek()) {
$fridays[] = $date->format('Y-m-d');
}

关于php - 如何通过碳获取日期范围内的所有星期五,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com