<?php
class Calendar
{
	public function getCalendar($month = null, $year = null, $weekStart = 1)
	{
		$date = new Zend_Date();
		
		$month = (int) $date->get(Zend_Date::MONTH);
		$weekday = (int) $date->get(Zend_Date::WEEKDAY_DIGIT);
		$day =(int) $date->get(Zend_Date::DAY);
		$numberDays =(int) $date->get(Zend_Date::MONTH_DAYS);
		
		if ($day == 1) {
			$weekday--;
		}

		while ($day != 1) {
			
			$day--;
			
			if (!$weekday) {
				$weekday = 6;
			} else {
			$weekday--;
			}
		}
		
		if ($weekStart == 0) {
			$weekday++;
		}
		
		$calendar = array();
		$counter = 1;
		$flag = 1;
		$firstWeek = true;
		$noInc = false;
		
		while ($counter <= $numberDays) {
			
			$dayPointer = 0;
			$week = array();
			while ($dayPointer <= 6) {
				
				if ($counter > $numberDays) {
					$week[] = '';
				} else {
					if ($firstWeek && $flag <= $weekday) {
						$week[] = '';
						$noInc = true;
					} else {
						$week[] = $counter;
						$noInc = false;
					}					
				}
				
				$dayPointer++;
				$flag++;
				if (!$noInc) $counter++;
			}
			$firstWeek = false;
			
			$calendar[] = $week; 	
		} 
		
		
		$result['calendar'] = $calendar;
		$result['this_day'] = $day;
		$result['start'] = ($weekStart == 1) ? 'mon' : 'sun';
		
		return $result;
	}
}
