<?php
function setDataOraIT() {
	setlocale(LC_TIME, 'it_IT.ISO_8859-1');
	$data=date ("d-m-Y H:i:s");
	return($data);
}

function setDataEstesa() {
	setlocale(LC_TIME, 'it_IT.ISO_8859-1');
	$data=date("dmY");
	return($data);
}

function setDataIT() {
	setlocale(LC_TIME, 'it_IT.ISO_8859-1');
	$data=date ("d-m-Y");
	return($data);
}

function datediff($tipo, $partenza, $fine) {
	switch ($tipo) {
		case "A" : $tipo = 365;
		break;
		case "M" : $tipo = (365 / 12);
		break;
		case "S" : $tipo = (365 / 52);
		break;
		case "G" : $tipo = 1;
		break;
	}
	$arr_partenza = explode("-", $partenza);
	$partenza_gg = $arr_partenza[0];
	$partenza_mm = $arr_partenza[1];
	$partenza_aa = $arr_partenza[2];
	$arr_fine = explode("-", $fine);
	$fine_gg = $arr_fine[0];
	$fine_mm = $arr_fine[1];
	$fine_aa = $arr_fine[2];
	$date_diff = mktime(12, 0, 0, $fine_mm, $fine_gg, $fine_aa) - mktime(12, 0, 0, $partenza_mm, $partenza_gg, $partenza_aa);
	$date_diff  = floor(($date_diff / 60 / 60 / 24) / $tipo);
	return $date_diff;
}

function formattaData($val) {
	if (stripos($val,"-") > 0 || stripos($val,"/") > 0) {
		$g= strtok($val,"-/");
		$m= strtok("-/");
		$a= strtok("-/");
		$data = $a.$m.$g;
	} else {
		$g= substr($val,0,2);
		$m= substr($val,2,2);
		$a= substr($val,4,4);
		$data = $a.$m.$g;
	}
	return($data);
}

function formattaData2($val) {
	$a=substr($val,0,4);
	$m=substr($val,4,2);
	$g=substr($val,6,2);

	$data = $a.'-'.$m.'-'.$g;
	return($data);
}

function formattaDataOra($val) {
	$data='';
	if($val!='') {
		$a=substr($val,0,4);
		$m=substr($val,4,2);
		$g=substr($val,6,2);
		
		$hh=substr($val,9,2);
		$mm=substr($val,11,2);
		$ss=substr($val,13,2);
	
		$data = $a.'-'.$m.'-'.$g.'*'.$hh.':'.$mm.':'.$ss;
	}
	return($data);
}


function generaSpazi($quanti) {
	$i=0;
	$stringa='';
	for($i=0;$i<$quanti;$i++) {
		$stringa.=' ';
	}
	return($stringa);
}

/*
function formattaDurata($val) {
	if (stripos($val,".") > 0 || stripos($val,",") > 0) {
		$mm= str_replace(".","",strtok($val,".,"));
		$ss= str_replace(".","",strtok(".,"));
	} else {
		$mm=str_replace(".","",$val);
		$ss='00';
	}

	$mm=substr('000'.$mm,(strlen('000'.$mm)-3));
	$durata = $mm.$ss;
	return($durata);
}
*/

function formattaDurata($val) {
	$hh = 0;
	$mm = 0;
	$ss = 0;
	
	if($val<=240000 && $val>=100000) {
		$hh = substr($val,(strlen($val)-6),strlen($val)-4);
		$mm = substr($val,(strlen($val)-4),(strlen($val)-4));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<100000 && $val>5900) {
		$hh = substr($val,(strlen($val)-5),strlen($val)-4);
		$mm = substr($val,(strlen($val)-4),(strlen($val)-3));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=5900 && $val>900) {
		$mm = substr($val,(strlen($val)-4),(strlen($val)-2));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=900 && $val>99) {
		$mm = substr($val, (strlen($val)-3),(strlen($val)-2));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=59 && $val>=10) {
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<10 && $val>0) {
		$ss = $val;
	}

	$hh = $hh*60*60;
	$mm = $mm*60;
	$durata=$hh+$mm+$ss;
	return($durata);
}


function formatOraDaDB($val) {
	$ss = '00';
	$mm = '00';
	$hh = '00';
	
	if($val<=240000 && $val>=100000) {
		$hh = substr($val,(strlen($val)-6),(strlen($val)-4));
		$mm = substr($val,(strlen($val)-4),(strlen($val)-4));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<100000 && $val>5900) {
		$hh = '0'.substr($val,(strlen($val)-5),(strlen($val)-4));
		$mm = substr($val,(strlen($val)-4),(strlen($val)-3));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=5900 && $val>900) {
		$hh = '00';
		$mm = substr($val,(strlen($val)-4),(strlen($val)-2));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=900 && $val>59) {
		$hh = '00';
		$mm = '0'.substr($val,(strlen($val)-3),(strlen($val)-2));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=59 && $val>=10) {
		$hh = '00';
		$mm = '00';
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<10 && $val>0) {
		$hh = '00';
		$mm = '00';
		$ss = '0'+$val;
	}
	$orario=$hh.':'.$mm.':'.$ss;
	return($orario);
}

/*
function checkStringa($stringa) {
	$stringa=str_replace("'","`",$stringa);
	$stringa=str_replace("°","",$stringa);
	$stringa=str_replace("À","A`",$stringa);
	$stringa=str_replace("È","E`",$stringa);
	$stringa=str_replace("Ì","I`",$stringa);
	$stringa=str_replace("Ò","O`",$stringa);
	$stringa=str_replace("Ù","U`",$stringa);
	$stringa=str_replace("Á","A`",$stringa);
	$stringa=str_replace("É","E`",$stringa);
	$stringa=str_replace("Í","I`",$stringa);
	$stringa=str_replace("Ó","O`",$stringa);
	$stringa=str_replace("Ú","U`",$stringa);
	return($stringa);
}
*/

function checkStringa($stringa) {
	$stringa=str_replace("'","`",$stringa);
	$stringa=str_replace("<","&lt;",$stringa);
	$stringa=str_replace(">","&gt;",$stringa);
	return($stringa);
}



function formatHHMMSS($durata) {
	$tmp=$durata/3600;
	if (strpos($tmp,"." )>0) $ore	 = substr($tmp,0,strpos($tmp,"."));
	else $ore=$tmp;
	if ($ore<10) $ore='0'.$ore;
	
	$tmp=(($durata-($ore*3600))/60);
	if (strpos($tmp,"." )>0) $minuti	 = substr($tmp, 0,strpos($tmp,"." ));
	else $minuti=$tmp;
	if ($minuti<10) $minuti='0'.$minuti;
	
	$tmp=$durata-($ore*3600)-($minuti*60);
	if (strpos($tmp,"." )>0) $secondi = substr($tmp, 0,strpos($tmp,"." ));
	else $secondi=$tmp;
	
	if ($secondi<10) $durata=$ore.':'.$minuti.':0'.$secondi;
	else  $durata=$ore.':'.$minuti.':'.$secondi;
	
	return($durata);
}

function formatHHMM($durata) {
	$tmp=$durata/3600;
	if (strpos($tmp,"." )>0) $ore	 = substr($tmp,0,strpos($tmp,"."));
	else $ore=$tmp;
	if ($ore<10) $ore='0'.$ore;
	
	$tmp=(($durata-($ore*3600))/60);
	if (strpos($tmp,"." )>0) $minuti	 = substr($tmp, 0,strpos($tmp,"." ));
	else $minuti=$tmp;
	if ($minuti<10) $minuti='0'.$minuti;
	
	$tmp=$durata-($ore*3600)-($minuti*60);
	if (strpos($tmp,"." )>0) $secondi = substr($tmp, 0,strpos($tmp,"." ));
	else $secondi=$tmp;
	
	if ($secondi<10) $durata=$ore.':'.$minuti;
	else  $durata=$ore.':'.$minuti;
	
	return($durata);
}


function formatFromHHMMSS($val) {
	$ss = '00';
	$mm = '00';
	$hh = '00';
	
	if($val<=240000 && $val>=100000) {
		$hh = substr($val,(strlen($val)-6),(strlen($val)-4));
		$mm = substr($val,(strlen($val)-4),(strlen($val)-4));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<100000 && $val>5900) {
		$hh = '0'.substr($val,(strlen($val)-5),(strlen($val)-4));
		$mm = substr($val,(strlen($val)-4),(strlen($val)-3));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=5900 && $val>900) {
		$hh = '00';
		$mm = substr($val,(strlen($val)-4),(strlen($val)-2));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=900 && $val>59) {
		$hh = '00';
		$mm = '0'.substr($val,(strlen($val)-3),(strlen($val)-2));
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<=59 && $val>=10) {
		$hh = '00';
		$mm = '00';
		$ss = substr($val,(strlen($val)-2),strlen($val));
	} else if($val<10 && $val>0) {
		$hh = '00';
		$mm = '00';
		$ss = '0'+$val;
	}
	
	$ora=($hh*3600) + ($mm*60) + $ss;
	return($ora);
}

function formatFromHHMM($val) {
	$ora=0;
	$arr=explode(":",$val);
	
	if(substr($arr[0],0,1)=='0') $arr[0]=substr($arr[0],1,1);
	if(substr($arr[1],0,1)=='0') $arr[1]=substr($arr[1],1,1);		

	$hh	= ($arr[0]*3600);
	$mm	= ($arr[1]*60);
	
	$ora=(($hh+$mm)*1000);
	return($ora);
}


function recursive_remove_directory($directory, $empty=FALSE) {
	if(substr($directory,-1) == '/') {
		$directory = substr($directory,0,-1);
	}
	if(!file_exists($directory) || !is_dir($directory)) {
		return FALSE;
	}elseif(is_readable($directory)) {
		$handle = opendir($directory);
		while (FALSE !== ($item = readdir($handle))) {
			if($item != '.' && $item != '..') {
				$path = $directory.'/'.$item;
				if(is_dir($path))  {
					recursive_remove_directory($path);
				}else{
					unlink($path);
				}
			}
		}
		closedir($handle);
		if($empty == FALSE) {
			if(!rmdir($directory)) {
				return FALSE;
			}
		}
	}
	return TRUE;
}


function cropImg($img,$filename){
	$d=120;
	$image = imagecreatefromjpeg($img);

	$width = imagesx($image);
	$height = imagesy($image);
	
	if($width<=$height && $width>$d) {
		$n_height=($d*$height)/$width;
		$n_width=$d;
	} else {
		$n_width=($d*$width)/$height;
		$n_height=$d;
	}
	
	$thumb = imagecreatetruecolor($d, $d);
	imagecopyresampled($thumb,
					   $image,
					   0 - ($n_width - $d) / 2, // Center the image horizontally
					   //0 - ($n_height - $d) / 2, // Center the image vertically
					   0,
					   0, 0,
					   $n_width, $n_height,
					   $width, $height);
	imagejpeg($thumb, $filename, 80);
}
/*
	$image = imagecreatefromjpeg($img);

	$width = imagesx($image);
	$height = imagesy($image);
	
	$newheight=$height*(100/$width);
	
	if($newheight>100) {
		$newheight=100;
		$height2=$width;
	} else {
		$height2=$height;
		
	}
	
	$thumb = imagecreatetruecolor(100, $newheight);
	imagecopyresampled($thumb, $image, 0, 0, 0, 0, 100, $newheight, $width, $height2);
	imagejpeg($thumb, $filename, 80);
}
*/
/*	
	$thumb_width = 120;
	$thumb_height = 120;
	
	$width = imagesx($image);
	$height = imagesy($image);
	
	$original_aspect = $width / $height;
	$thumb_aspect = $thumb_width / $thumb_height;
	
	if($original_aspect >= $thumb_aspect) {
	   // If image is wider than thumbnail (in aspect ratio sense)
	   $new_height = $thumb_height;
	   $new_width = $width / ($height / $thumb_height);
	} else {
	   // If the thumbnail is wider than the image
	   $new_width = $thumb_width;
	   $new_height = $height / ($width / $thumb_width);
	}
	
	$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
	
	// Resize and crop
	imagecopyresampled($thumb,
					   $image,
					   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
					   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
					   0, 0,
					   $new_width, $new_height,
					   $width, $height);
	imagejpeg($thumb, $filename, 80);}
*/
?>