查找一个月的每周时段(从星期一开始) [英] Find weekly periods (starting on a Monday) for a month
问题描述
我正在尝试查找给定月份和年份的每周时段.日期应从星期一开始,到星期日结束.如果每月的1号是星期日(例如2011年5月),则它应该是第一个元素.
I'm trying to find the weekly periods for a given month and year. Dates should start on a Monday and end on a Sunday. If the 1st of the month is a Sunday (Ex May 2011), it should be the first element.
2011年5月
- 5月1日(星期日)
- 5月2日-5月8日(星期一-星期日)
- 5月9日-5月15日(星期一-星期日)
- 5月17日-6月22日(星期一-星期日)
- 5月23日-5月29日(星期一-星期日)
- 5月30日-5月31日(星期一-星期二)
2012年9月
- 9月1日至9月2日
- 9月3日-9月9日
- 9月10日-9月16日
- 9月17日至9月23日
- 9月24日-9月30日
我正在使用此功能来计算两个日期的星期数-即该月的第一天和该月的最后一天.
I am using this function to calculate the week numbers for two dates - I.e. the 1st day of the month and last day of the month.
public function getWeekNumbers($startDate, $endDate)
{
$p = new DatePeriod(
new DateTime($startDate),
new DateInterval('P1W'),
new DateTime($endDate)
);
$weekNumberList = array();
foreach ($p as $w)
{
$weekNumber = $w->format('W');
$weekNumberList[] = ltrim($weekNumber, '0');
}
return $weekNumberList;
}
奇怪的是,对于1月份,当我期望为[1、2、3、4、5]时,它返回的周数为[52、1、2、3、4].
Strangely, for the month of January, it returns week numbers of [52, 1, 2, 3, 4] when I'm expecting [1, 2, 3, 4, 5].
一旦我有了周数,我就会像这样使用它们:
Once I have the week numbers, I'm using them like so:
//The following loop will populate the dataset with the entire month's durations - regardless if hours were worked or not.
$firstDayOfMonth = date('Y-m-d', strtotime("first day of {$this->year}-{$monthName}"));
$lastDayOfMonth = date('Y-m-d', strtotime("last day of {$this->year}-{$monthName}"));
foreach ($this->getWeekNumbers($firstDayOfMonth, $lastDayOfMonth) as $key => $weekId)
{
// find first mоnday of the year
$firstMon = strtotime("mon jan {$this->year}");
// calculate how many weeks to add
$weeksOffset = $weekId - date('W', $firstMon);
$beginDays = $weeksOffset * 7;
$endDays = ($weeksOffset * 7) + 6;
$searchedMon = strtotime(date('Y-m-d', $firstMon) . " +{$beginDays} days");
$searchedSun = strtotime(date('Y-m-d', $firstMon) . " +{$endDays} days");
echo date("M d", $searchedMon) . " - " . date("M d", $searchedSun);
}
由于getWeekNumbers函数未返回我期望的星期数,因此上述函数的输出并不奇怪
Since, the getWeekNumbers function isn't returning the week numbers I'm expecting, it's not surprising that the output of the above function is
- 2012年12月24日至12月30日
- 2012年1月2日至1月8日
- 2012年1月9日至1月15日
- 2012年1月16日至1月22日
- 2012年1月23日至1月29日
请注意,第一行(12月24日至12月30日)是当年(2012年)的结束日期,而不是去年(2011年)的结束日期.
Note that the 1st line (Dec 24 - Dec 30) is the end of the current year (2012) and not the end of last year (2011).
理想情况下,我希望它看起来像
Ideally, I want it to look like
有什么想法吗?谢谢!
推荐答案
如果您需要选定月份的所有星期,以及选定星期的所有日期,那么这就是您所需要的:
If you need all weeks for selected month, and all dates for selected week, then this is all you need:
function getWeekDays($month, $year)
{
$p = new DatePeriod(
DateTime::createFromFormat('!Y-n-d', "$year-$month-01"),
new DateInterval('P1D'),
DateTime::createFromFormat('!Y-n-d', "$year-$month-01")->add(new DateInterval('P1M'))
);
$datesByWeek = array();
foreach ($p as $d) {
$dateByWeek[ $d->format('W') ][] = $d;
}
return $dateByWeek;
}
getWeekDays()函数返回多维数组.第一个关键是星期几. 2级是数组,其日期保存为DateTime对象.
getWeekDays() function returns multi dimension array. first key is week number. 2 level is array, that has dates saved as DateTime object.
获取示例:
print_r( getWeekDays(5, 2011) ); # May 2011
print_r( getWeekDays(9, 2012) ); # Sep 2012
我有一点点额外的时间,所以我写了一个例子;-)
I had a little time extra, so I written an example ;-)
$datesByWeek = getWeekDays(8, 2012);
$o = '<table border="1">';
$o.= '<tr><th>Week</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th></tr>';
foreach ($datesByWeek as $week => $dates) {
$firstD = $dates[0];
$lastD = $dates[count($dates)-1];
$o.= "<tr>";
$o.= "<td>" . $firstD->format('M d') . ' - ' . $lastD->format('M d') . "</td>";
$N = $firstD->format('N');
for ($i = 1; $i < $N; $i++) {
$o.= "<td>-</td>";
}
foreach ($dates as $d) {
$o.= "<td>" . $d->format('d.') . " / 0.00</td>";
# for selected date do you magic
}
$N = $lastD->format('N');
for ($i = $N; $i < 7; $i++) {
$o.= "<td>-</td>";
}
$o.= "</tr>";
}
$o.= '</table>';
echo $o;
输出看起来像:
这篇关于查找一个月的每周时段(从星期一开始)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!