<?php
		error_reporting(0);
		include('database.php');
		DB_connexion(); 

function compte_jour($debut, $fin)
{
        //60 secondes X 60 minutes X 24 heures dans une journée
        $nbSecondes= 60*60*24;
 
        $debut_ts = strtotime($debut);
        $fin_ts = strtotime($fin);
        $diff = $fin_ts - $debut_ts;
        return round($diff / $nbSecondes);
}

function calcul_timestamp($date)
{ 
	list($jour, $mois, $annee) = explode('-', $date);
	$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
	return($timestamp);
}
	
	$tab_debut_annee = ["01-01-2015","01-01-2016","01-01-2017"];
	$tab_ouverture = ["15-02-2015","15-02-2016","15-02-2017"];
	$tab_fermeture = ["15-11-2015","15-11-2016","15-11-2017"];
	$tab_fin_annee = ["31-12-2015","31-12-2016","31-12-2017"];
	
	
	$nb_an = count($tab_debut_annee);
	
	for($i = 0 ; $i < $nb_an; $i++)
	{
		$debut_annee = $tab_debut_annee[$i];
		$ouverture = $tab_ouverture[$i];
		$fermeture = $tab_fermeture[$i];
		$fin_annee = $tab_fin_annee[$i];
		
		$nb_jour =  compte_jour($debut_annee, $ouverture);
		
		$start = $debut_annee;
		$array[] = $start;
		for($j = 1; $j < $nb_jour; $j++)
		{ $date = date("d-m-Y", strtotime($start." +1 days"));
		  $start = $date;
		  $array[] = $date;
		  $array_stamp[] = calcul_timestamp($date);
		}

		$nb_jour =  compte_jour($fermeture, $fin_annee);
		$start = $fermeture;
		$array[] = $start;
		for ($j = 0; $j < $nb_jour; $j++)
		{ $date = date("d-m-Y", strtotime($start." +1 days"));
		  $start = $date;
		  $array[] = $date;
  		  $array_stamp[] = calcul_timestamp($date);
		}
	}	
	
	/*$user = "root";
	$password = "";
	$host = "localhost";
	$db = "test";

	$db_connection = mysql_connect($host, $user, $password);
	mysql_select_db($db, $db_connection); 
	
	if(!$db_connection)
	{
		die('Could not connect: ' . mysql_error());
	}
 */
	mysql_select_db($db, $db_connection); 
	$sql = 'SELECT * FROM reservation WHERE statut = "reserver"';
	$result = mysql_query($sql);	
	
	while ($data = mysql_fetch_array($result))
	{
		$start =  $data['debut'];
		$stop = $data['depart'];
				
		$nb_jour =  compte_jour($start, $stop);
		$array[] = $start;	
		
		for ($i = 1; $i <= $nb_jour; $i++)
		{ $date = date("d-m-Y", strtotime($start." +1 days"));
		  $start = $date;
		  $array[] = $date;
  		  $array_stamp[] = calcul_timestamp($date);

		}
	}
	
	/*sort($array_stamp);
	
	$n = count($array_stamp);
	
	for ($i = 0 ; $i < $n; $i++)
	{ 	$timestamp = $sort[$i];
		$dt = date('d/m/Y', $timestamp);
		$sort_array[] = $dt;
		//echo $dt;
		//echo '<br>';
	}*/
	
	$json_jour_non_disponible =  json_encode($array); 
	echo $json_jour_non_disponible;
	

	


?>