/** * Return an array of the timestamps of the start of each week that falls partially in the * provided month. monday is the first day of the week. * @param integer $month The week number of the year * @param integer $year The year of the week we need to calculate on * @return array of integers, each a unix timestamp * @author tim@e2-media.co.nz */ function month_get_weeks($month, $year) { //////////////////////////////////////////////////// // get an array of all weeknums falling in the month $weeknums = array(); // go through all days in month for($day=1;$day<32;$day++) { // calculate timestamp $ts = mktime(0,0,0,$month,$day,$year); // is it inside the month? if not, skip to end if(date('n',$ts) != $month) continue; // calculate weeknum for this day $weeknum = date('W',$ts); // adjust weeknum if right at start of year $dayofyear = date('z',$ts); $dayofweek = (date('w',$ts)+6)%7; // adjustment because 'w' is 0-based at Sunday if($dayofweek >= $dayofyear) $weeknum = 0; $weeknums[] = $weeknum; } // trim dupes $weeknums = array_unique($weeknums); ///////////////////////////////////////////////////// // build array of start timestamps $weekstarts = array(); foreach($weeknums as $weeknum) { $weekstarts[$weeknum] = find_first_day_ofweek($weeknum, $year); } return $weekstarts; } /** * Function to find the day of a week in a year * @param integer $week The week number of the year * @param integer $year The year of the week we need to calculate on * @param string $start_of_week The start day of the week you want returned * Monday is the default Start Day of the Week in PHP. For * example you might want to get the date for the Sunday of wk 22 * @return integer The unix timestamp of the date is returned * @author from jefferyfernandez.id.au http://nz.php.net/manual/en/function.mktime.php */ function find_first_day_ofweek($week, $year) { // Get the target week of the year with reference to the starting day of // the year $target_week = strtotime("$week week", strtotime("1 January $year")); // Get the date information for the day in question which // is "n" number of weeks from the start of the year $date_info = getdate($target_week); // Get the day of the week (integer value) $day_of_week = $date_info['wday']; $adjusted_date = $day_of_week-1; // Get the first day of the weekday requested $first_day = strtotime("-$adjusted_date day",$target_week); //return date('l dS of F Y h:i:s A', $first_day); return $first_day; }