如何获取给定月份的每周的日期间隔? [英] How to get the date interval for each week in a given month?
本文介绍了如何获取给定月份的每周的日期间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一种方法可以在一个月的PHP中获取每周的开始/结束日期吗?
所以例如,如果我采取这个月(2011年11月),我需要返回
Is there a way to get the date start/end for each week in PHP given a month? So for example if i take this month(Nov 2011) i need to return
30 Oct -> 5 Nov
6 Nov -> 12 Nov
13 Nov -> 19 Nov
20 Nov -> 26 Nov
27 Nov -> 3 Dec
LE:
我做了,以防其他人需要它。代码不漂亮,但它的工作原理我需要它。
Here's what i made in case some one else needs it. The code is not pretty but it works as i need it.
public function getMonthWeeks($month, $year)
{
$month = intval($month);
if ($month < 1) {
$month = 1;
}
if ($month > 12) {
$month = 12;
}
$tsfdOfMonth = mktime(0, 0, 0, $month, 1, $year);
$dayOfWeek = idate('w', $tsfdOfMonth);
$tIntervalStart = $tsfdOfMonth;
$tNextMonth = idate('m', strtotime("+1 month", $tsfdOfMonth));
if ($dayOfWeek > 0) {
$tStr = sprintf("-%d %s",
$dayOfWeek,
$dayOfWeek == 1 ? 'day' : 'days'
);
$tIntervalStart = strtotime($tStr, $tsfdOfMonth);
}
$resultDates = array();
$tsStart = $tIntervalStart;
$tsEnd = strtotime('+6 days', $tsStart);
while (true) {
$rObj = new stdClass;
$rObj->LinkStr = sprintf("%s %s - %s %s",
date('M', $tsStart), date('d', $tsStart),
date('M', $tsEnd), date('d', $tsEnd)
);
$rObj->DateStart = date('Y-m-d', $tsStart);
$rObj->DateEnd = date('Y-m-d', $tsEnd);
$resultDates[] = $rObj;
if (idate('m', strtotime('+1 day', $tsEnd)) == $tNextMonth) {
break;
}
$tsStart = strtotime('+1 day', $tsEnd);
$tsEnd = strtotime('+6 days', $tsStart);
}
return $resultDates;
}
推荐答案
你将不得不写一个功能:
You will have to write a function:
算法:
- 获取月份的星期几。
- 减去必要的天数到达星期天 - 您现在有月份的第一个星期天的日期(可能是上个月的上个月)。
- 添加六天以确定该周的星期六。
- 添加一天以确定下周的星期天。
- 重复3和4,直到我们不再在当前月份
- Get day of week of first of month.
- Subtract necessary number of days to reach a Sunday--you now have date of the first "sunday" of the month (possibly last sunday of previous month).
- Add six days to determine the Saturday of that week.
- Add one day to determine the Sunday of the next week.
- repeat 3 and 4 until we are no longer in the current month
两个功能将非常有帮助: $ saturdaytimestamp = strtotime(+ 6天,$ sundaytimestamp)
和 $ dayofweek = idate('w',$ timestamp);
Two features will be very helpful: $saturdaytimestamp = strtotime("+6 days", $sundaytimestamp)
and $dayofweek = idate('w', $timestamp);
这篇关于如何获取给定月份的每周的日期间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文