get array of weeks, by month & year

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

get array of weeks, by month & year

Post by penguinboy »

Code: Select all

<?php
function get_weeks($year,$month=1,$day=1,$display_weeks=2)
{
	for($i=0; $i<$display_weeks; $i++)
	{
		$week = array();
		
		$sunday	= ($day - date('w',mktime(0,0,0,$month,$day,$year)));
		
		for($x=0; $x<7; $x++)
			$week[] = date('Ymd',mktime(0,0,0,$month,$sunday+$x,$year));
		
		$calendar[] = $week;
		
		$day = $sunday+7;
	}
	
	return $calendar;
}

$weeks = get_weeks(2004,1,1,2);

print '<pre>';
print_r($weeks);

/**
Array
(
    [0] => Array
        (
            [0] => 20031228
            [1] => 20031229
            [2] => 20031230
            [3] => 20031231
            [4] => 20040101
            [5] => 20040102
            [6] => 20040103
        )

    [1] => Array
        (
            [0] => 20040104
            [1] => 20040105
            [2] => 20040106
            [3] => 20040107
            [4] => 20040108
            [5] => 20040109
            [6] => 20040110
        )

)
*/
?>
Post Reply