Skip to content

Instantly share code, notes, and snippets.

@davialexandre
Created May 17, 2012 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davialexandre/2721369 to your computer and use it in GitHub Desktop.
Save davialexandre/2721369 to your computer and use it in GitHub Desktop.
Function to get weeks for a given month
<?php
function getWeeksForMonth($year, $month) {
$weeks = array();
$weekday_of_first_day = date('w', strtotime("$year-$month-1"));
$last_day_of_first_week = 6 - $weekday_of_first_day + 1;
$weeks[] = array("$year-$month-1", "$year-$month-$last_day_of_first_week");
$last_day_of_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$day = $last_day_of_first_week;
while($day < $last_day_of_month) {
$first_day_of_week = ($day += 1);
$day += 6;
if($day >= $last_day_of_month) {
$last_day_of_week = $last_day_of_month;
} else {
$last_day_of_week = $day;
}
$weeks[] = array("$year-$month-$first_day_of_week", "$year-$month-$last_day_of_week");
}
return $weeks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment