gpt4 book ai didi

php - 如何获得两个时间间隔之间的星期五日期

转载 作者:可可西里 更新时间:2023-11-01 00:55:06 24 4
gpt4 key购买 nike

我想从两个时间间隔中获取所有星期五的日期。我的框架是 laravel。

我的功能:

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;
}

但是从两个日期返回:

"["2017-12-25","2018-01-01","2018-01-08","2018-01-15","2018-01-22","2018-01-29","2018-02-05","2018-02-12","2018-02-19","2018-02-26","2018-03-05","2018-03-12","2018-03-19","2018-03-26","2018-04-02","2018-04-09","2018-04-16","2018-04-23","2018-04-30","2018-05-07","2018-05-14","2018-05-21","2018-05-28","2018-06-04","2018-06-11","2018-06-18","2018-06-25","2018-07-02","2018-07-09","2018-07-16","2018-07-23","2018-07-30","2018-08-06","2018-08-13","2018-08-20","2018-08-27","2018-09-03","2018-09-10","2018-09-17","2018-09-24","2018-10-01","2018-10-08","2018-10-15","2018-10-22","2018-10-29","2018-11-05","2018-11-12","2018-11-19","2018-11-26","2018-12-03","2018-12-10","2018-12-17","2018-12-24","2018-12-31"]"

我的问题在哪里?

最佳答案

你可以使用 Carbon:

$dateTo = Carbon::parse($dateToString);
$friday = Carbon::parse($dateFromString . ' next friday');
$fridays = [];

while($friday->lt($dateTo)) {
$fridays[] = $friday->toDateString();
$friday->addWeek();
}

return $fridays;

关于php - 如何获得两个时间间隔之间的星期五日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48044788/

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