<?php

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);
}


	error_reporting(0);

	$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); 
	//$query = "SELECT * FROM reservation where status LIKE 'louer'  ";
	$sql = 'SELECT * FROM reservation_2015 WHERE statut = "reserver"';
	$result = mysql_query($sql);	
	/*$data = mysql_fetch_array($result);
	echo $data['depart'];*/
	
	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[] = "01-09-2015";
	
	$sql = 'SELECT * FROM ouverture_fermeture_2015';
	$result = mysql_query($sql);	
	$data = mysql_fetch_array($result);
	//echo $data['ouverture'];	
	//echo $data['fermeture'];	
	
	$debut_annee = "01-01-2015";
	$ouverture =  $data['ouverture'];
	$fermeture = $data['fermeture'];
	$fin_annee = "31-12-2015";
	
	$nb_jour =  compte_jour($debut_annee, $ouverture);
		
	$start = $debut_annee;
	$array_avant[] = $start;
	for ($i = 1; $i < $nb_jour; $i++)
		{ $date = date("d-m-Y", strtotime($start." +1 days"));
		  $start = $date;
		  $array_avant[] = $date;
		}

	$nb_jour =  compte_jour($fermeture, $fin_annee);
		
	$start = $fermeture;
	$array_apres[] = $start;
	for ($i = 1; $i <= $nb_jour; $i++)
		{ $date = date("d-m-Y", strtotime($start." +1 days"));
		  $start = $date;
		  $array_apres[] = $date;
		}
		
	 //$array[0] = "10-11-2014";
	 //$array[1] = "21-12-2014";
	 //$array[2] = "12-01-2015";

	/*echo "<pre>";
	 print_r($array);
	 echo "</pre>";*/

	
	$result = array_merge($array_avant, $array, $array_apres );
	//print_r($result);
	
	$jsonConfirmedSends =  json_encode($result); 
    echo $jsonConfirmedSends;
	
/*

/*
CREATE TABLE ouverture_fermeture_2015 (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
	ouverture varchar(10),
    fermeture varchar(10),
    PRIMARY KEY (id)
)
CREATE TABLE reservation (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
	arrive varchar(10),
    depart varchar(10),
    status varchar(10),
    PRIMARY KEY (id)
) */




	
?>	
