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:
Algorithm:
Two features will be very helpful:
$saturdaytimestamp = strtotime("+6 days", $sundaytimestamp)and$dayofweek = idate('w', $timestamp);