gpt4 book ai didi

php - 获取一个月中的周数和对应的天数

转载 作者:行者123 更新时间:2023-12-05 09:22:40 25 4
gpt4 key购买 nike

我很确定我把这个复杂化了,但我不知道如何用另一种方式来做。

我想创建一个数组,其中包含一个月中的周数作为键,包含本周的天数作为值。

我正在使用此函数获取一周中的所有天数:

public function getDaysOfWeek($year, $month, $day)
{
$firstMondayThisWeek = new DateTime($year . '/' . $month . '/' . $day, new DateTimeZone("Europe/Berlin"));
$firstMondayThisWeek->modify('tomorrow');
$firstMondayThisWeek->modify('last Monday');

$nextSevenDays = new DatePeriod(
$firstMondayThisWeek,
DateInterval::createFromDateString('+1 day'),
6
);

return $nextSevenDays;
}

以及构建数组的函数:

public function getWeekAndDays($year, $month)
{
$weeksInMonth = Carbon::createFromDate($year, $month)->endOfMonth()->weekOfMonth;
$weekBegin = Carbon::createFromDate($year, $month)->startOfMonth();

$weeks = [];

for($i=1; $i<=$weeksInMonth; $i++)
{
$collection = new \stdClass();
$collection->week = $i;
$collection->days = $this->getDaysOfWeek($year, $month, $weekBegin->day);

$weekBegin->addWeek(0);

$weeks[] = $collection;
}

return $weeks;
}

除 2 月外,所有月份我都有 5 周的时间。 2 月份我有 4 周的时间,所以我无法保存月份重叠的日子。

我在这里完全错了吗?解决此任务的可能方法是什么?

最佳答案

我的 friend 我感觉到你的痛苦,处理日历数据很烦人。这是我不久前写的一个函数,它构建了一个以月为间隔的周和天数组。这不是最干净的代码,但它可以工作。

它将从您在 $today 中作为“Y-m-d”(或默认为当前日期)传递的日期开始,然后回到当月的第一周,从那里开始,然后然后继续 $scheduleMonths 个月构建一个数组,首先按月索引,然后按周索引。

这里有点难以解释,但它是独立的,因此您只需将其复制/粘贴到您的代码中,然后 dd() 输出以查看它的外观以及是否正确为你工作,并从那里修改它。您可能需要调整一些格式,因为它是针对我的特定用例以这种方式完成的,但业务逻辑应该是合理的。

您应该能够从输出中提取给定月份的周数(因此,如果这就是您所需要的,您可以在确认它有效后简化它)。

public function getWeeks($today = null, $scheduleMonths = 6) {

$today = !is_null($today) ? Carbon::createFromFormat('Y-m-d',$today) : Carbon::now();

$startDate = Carbon::instance($today)->startOfMonth()->startOfWeek()->subDay(); // start on Sunday
$endDate = Carbon::instance($startDate)->addMonths($scheduleMonths)->endOfMonth();
$endDate->addDays(6 - $endDate->dayOfWeek);

$epoch = Carbon::createFromTimestamp(0);
$firstDay = $epoch->diffInDays($startDate);
$lastDay = $epoch->diffInDays($endDate);

$week=0;
$monthNum = $today->month;
$yearNum = $today->year;
$prevDay = null;
$theDay = $startDate;
$prevMonth = $monthNum;

$data = array();

while ($firstDay < $lastDay) {

if (($theDay->dayOfWeek == Carbon::SUNDAY) && (($theDay->month > $monthNum) || ($theDay->month == 1))) $monthNum = $theDay->month;
if ($prevMonth > $monthNum) $yearNum++;

$theMonth = Carbon::createFromFormat("Y-m-d",$yearNum."-".$monthNum."-01")->format('F Y');

if (!array_key_exists($theMonth,$data)) $data[$theMonth] = array();
if (!array_key_exists($week,$data[$theMonth])) $data[$theMonth][$week] = array(
'day_range' => '',
);

if ($theDay->dayOfWeek == Carbon::SUNDAY) $data[$theMonth][$week]['day_range'] = sprintf("%d-",$theDay->day);
if ($theDay->dayOfWeek == Carbon::SATURDAY) $data[$theMonth][$week]['day_range'] .= sprintf("%d",$theDay->day);

$firstDay++;
if ($theDay->dayOfWeek == Carbon::SATURDAY) $week++;
$theDay = $theDay->copy()->addDay();
$prevMonth = $monthNum;
}

$totalWeeks = $week;

return array(
'startDate' => $startDate,
'endDate' => $endDate,
'totalWeeks' => $totalWeeks,
'schedule' => $data,
);

}

我希望这至少能帮助您入门!

关于php - 获取一个月中的周数和对应的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25199465/

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