<?php	
//dopelnienie groszy funkcaj doaej 0 do setek po przecinku jezeli nie ma
function d_gr($kwota)
{
	$kwota_tmp = explode('.',$kwota);
	if(empty($kwota_tmp[0])) $kwota_tmp[0] = 0; //jezeli 0 zlotych
	if(empty($kwota_tmp[1])) $kwota_tmp[1] = 0; //jezeli 0 groszy
	if(strlen($kwota_tmp[1])==1) $kwota_tmp[1]= $kwota_tmp[1].'0';
	
	return $kwota_tmp;
}

//funkcja oblicza poczatek tygodnia i koniec tygodnia jezeli poda sie rok i numer tygodnia z roku np (16,2008)
function WeekToDate ($week, $year)
{
	$Jan1 = mktime (1, 1, 1, 1, 1, $year);
	$iYearFirstWeekNum = (int) strftime("%W",mktime (1, 1, 1, 1, 1, $year));
	
	if ($iYearFirstWeekNum == 1)
	{
	$week = $week - 1;
	}
	
	$weekdayJan1 = date ('w', $Jan1);
	$FirstMonday = strtotime(((4-$weekdayJan1)%7-3) . ' days', $Jan1);
	$CurrentMondayTS = strtotime(($week) . ' weeks', $FirstMonday);
	
	$first = strftime("%m-%d-20%y",$CurrentMondayTS);
	
	$day = substr($first,3,2);
	$month = substr($first,0,2);
	$w = date('w',mktime(0,0,0,$month,$day,$year));	
	$end_date = mktime(0, 0, 0, $month, $day-$w+7 , $year);
	
	$last = strftime("20%y-%m-%d",$end_date); 
		
	$tydzien = array();
	
	$tydzien['r-m-d']['start'] =  strftime("20%y-%m-%d",$CurrentMondayTS);
	$tydzien['r-m-d']['end']   =  $last;
	
	//echo $first.'----'.$last;
	return $tydzien;
}

//funckaja zwracajaca tablice z liczba tygodni dla konkretnego roku i miesiaca
//zwracane sa wszystkie lata poczawszy od roku podanego w parametrze funkcji do roku obecnego 
function how_many_weeks_in_months($year=2007)
{	
		$month = 1;			
		$start_month = 1;
		$cur_year = date("Y");	  	
		$weeks_in_month = array();
		$week_quantity = 1;		
		  
		  for($i=$year; $i<=$cur_year;$i++)
		  {								  	
			$weeks_in_year = strftime("%W",strtotime("12/31/".$year));			
			$weeks_in_month[$year] = array();			
			 							
			for($j=1;$j<=$weeks_in_year;$j++)
			{
				$daty = WeekToDate($j,$year);				
				$month = (int) substr($daty['r-m-d']['start'],5,2);				
				
				if($start_month != $month)
				{
					$start_month = $month;
					$week_quantity = 1;					
				}
										
				$weeks_in_month[$year][$month] = $week_quantity++;															
			}			
			$year++;
		  }
		  
		  return $weeks_in_month; 
}

//funkcja zwraca poczatek i koniec miesiecznego okresu rozliczeniowego 
function month_period_start_end($start_month=10,$year=2009)
{			
		$month_period = array();
		$weeks_in_month = array();						  		  								  
		$weeks_in_year = strftime("%W",strtotime("12/31/".$year));			
						 							
		for($j=1;$j<=$weeks_in_year;$j++)
		{
			$daty = WeekToDate($j,$year);				
			$month = (int) substr($daty['r-m-d']['start'],5,2);				
			
			if($start_month == $month)
			{				
				$weeks_in_month[] = $daty;					
			}																				
		}

		$start = array_shift($weeks_in_month);
		$end = array_pop($weeks_in_month);
		$month_period = array('start'=>$start['r-m-d']['start'],'end'=>$end['r-m-d']['end']);
		
		return $month_period; 
}

//funkcja zwraca tablice z wszystkimi datami poczatku i konca tygodnia poczawszy od roku podanego w parametrze funkcji  oraz ilosc tygodni w mieisacu oraz numer tygodnia
function weeks_start_end_array($year=2007)
{
	$weeks = array();
	$cur_year = date("Y");
	$weeks_in_months = how_many_weeks_in_months($year);
					  						
	for($i=$year; $i<=$cur_year;$i++)
	{								  	
		$weeks_in_year = strftime("%W",strtotime("12/31/".$year));			
				 							
		for($j=1;$j<=$weeks_in_year;$j++)
		{	$week = WeekToDate($j,$year);
			$month = (int) substr($week['r-m-d']['start'],5,2);
			$week['r-m-d']['weeks_in_months'] = $weeks_in_months[$year][$month];
			$week['r-m-d']['week_number'] = $j;  
			$weeks[] = $week['r-m-d'];			  																																					
		}			
		$year++;
	}
	
	return	$weeks;							
}

//funkcja zwraca ostatni tydzien na dany rok
function last_week_of_year($year=2007)
{
	$weeks = array();
	$weeks_in_months = how_many_weeks_in_months($year);					  														  	
	$weeks_in_year = strftime("%W",strtotime("12/31/".$year));			
			 								
	$week = WeekToDate($weeks_in_year,$year);
	$month = (int) substr($week['r-m-d']['start'],5,2);
	$week['r-m-d']['weeks_in_months'] = $weeks_in_months[$year][$month];
	$week['r-m-d']['week_number'] = $weeks_in_year;  
	$weeks[] = $week['r-m-d'];			  																																					
					
	return	$weeks;							
}

//funkcja zwraca tygodnie na dany rok
function weeks_start_end_array_in_one_year($year=2007)
{
	$weeks = array();
	$weeks_in_months = how_many_weeks_in_months($year);					  														  	
	$weeks_in_year = strftime("%W",strtotime("12/31/".$year));			
			 							
	for($j=1;$j<=$weeks_in_year;$j++)
	{	$week = WeekToDate($j,$year);
		$month = (int) substr($week['r-m-d']['start'],5,2);
		$week['r-m-d']['weeks_in_months'] = $weeks_in_months[$year][$month];
		$week['r-m-d']['week_number'] = $j;  
		$weeks[] = $week['r-m-d'];			  																																					
	}			
	
	return	$weeks;							
}

//funkcja porownujaca daty (start > end return 1 else return 0)
function greaterDate($start_date,$end_date)
{
  $start = strtotime($start_date);
  $end = strtotime($end_date);
  if ($start-$end > 0)
    return 1;
  else
     return 0;
}


//funkcja zwraca poczatek i koniec ostatniego pelnego tygodnia w zaleznosci od dzisiejszej daty 
function last_week_start_end_pn_nd($year=2007)
{
	$weeks = array();
	$cur_year = date("Y");
	$weeks_in_months = how_many_weeks_in_months($year);
					  						
	for($i=$year; $i<=$cur_year;$i++)
	{								  	
		$weeks_in_year = strftime("%W",strtotime("12/31/".$year));			
				 							
		for($j=1;$j<=$weeks_in_year;$j++)
		{	
			$week = WeekToDate($j,$year);
			
			if(greaterDate(date('Y-m-d'),$week['r-m-d']['end']))
			{				
				$month = (int) substr($week['r-m-d']['start'],5,2);
				$week['r-m-d']['weeks_in_months'] = $weeks_in_months[$year][$month];
				$week['r-m-d']['week_number'] = $j;  
				$weeks = $week['r-m-d'];
			}			  																																					
		}			
		$year++;
	}
	return	$weeks;									
}

//funkcja zwraca numer dnia 
//za parametry przyjmuje:
//$occurence - ktore wystapienie danego dnia w miesiacu
//$name_of_day  - reprezentacja liczbowa dnia tygodnia 1-7 (np 1 - poniedzilek 7- niedziela)
//$month - miesaic w ktorym szukamy numeru dnia
//$year - rok w ktorym szuklamy numeru dnia
function date_from_nameday($occurrence, $name_of_day, $month, $year)
{
	$one_day = 24 * 60 *60;
	$one_week = 7 * $one_day;
	$one = mktime(0, 0, 0, $month, 1, $year);
	$two = $one + $one_day;
	$three = $two + $one_day;
	$four = $three + $one_day;
	$five = $four + $one_day;
	$six = $five + $one_day;
	$seven = $six + $one_day;
	$first = date('N', $one);
	$second = date('N', $two);
	$third = date('N', $three);
	$fourth = date('N', $four);
	$fifth = date('N', $five);
	$sixth = date('N', $six);
	$seventh = date('N', $seven);
	switch($name_of_day)
	{
	case $first: $first_occurrence = $one; break;
	case $second: $first_occurrence = $two; break;
	case $third: $first_occurrence = $three; break;
	case $fourth: $first_occurrence = $four; break;
	case $fifth: $first_occurrence = $five; break;
	case $sixth: $first_occurrence = $six; break;
	case $seventh: $first_occurrence = $seven; break;
	}
	$date_of_occurrence = ($occurrence > 1) ? $first_occurrence + ($one_week * ($occurrence-1)) : $first_occurrence;
	$day_to_return = date('d', $date_of_occurrence);
	$month_of_day = date('m', $date_of_occurrence);
	if ($month_of_day != $month)
	return -1;
	else
	return $day_to_return;
}
?>