获取一个月中特定一周的日期范围



我正在使用Laravel,我想在输入一个月的周顺序时生成一个日期列表。例如,当输入为"2nd week of October 2021"时;(或者简称为$week=2,$month = 10,$year = 2021),那么它将返回日期从10月10日到10月16日。有人能帮帮我吗?

您试过whereBetween了吗?例如:

$from = date('2021-01-01');
$to = date('2021-05-02');
Model::whereBetween('reservation_from', [$from, $to])->get();

检查以下函数。我想这样可以得到你想要的结果。

public function getWeekDates($year, $month, $week) {
$dt = Carbon::create($year, $month, 1);
$numberOfWeeks = floor($dt->daysInMonth / Carbon::DAYS_PER_WEEK);
if(($numberOfWeeks + 1) < $week) {
return response()->json('Error : Invalid week provided.', 400);
}
$monthStartOfWeek = $dt->startOfWeek()->format('Y-m-d');
$monthEndOfWeek = $dt->endOfWeek()->format('Y-m-d');
$addDays = ($week-1) * Carbon::DAYS_PER_WEEK;
$dt = $dt->addDays($addDays);
$startOfWeek = $dt->startOfWeek()->format('Y-m-d');
$endOfWeek = $dt->endOfWeek()->format('Y-m-d');
$result = [
'year' => $year,
'month' => $month,
'week' => $week,
'start_of_week' => $startOfWeek,
'start_of_week' => $startOfWeek,
'end_of_week' => $endOfWeek,
];

return $result;
}

相关内容

  • 没有找到相关文章

最新更新