<?php
function saison() {

	$date = date('Y/m/d');

      $limits= array('/12/21'=>'Hiver',
                     '/09/21'=>'Automne',
                     '/06/21'=>'Ete',
                     '/03/21'=>'Printemps',
                     '/01/01'=>'Hiver');

      foreach ($limits AS $key => $value) {
          $limit=date("Y").$key;
          if (strtotime($date)>=strtotime($limit)) {
              return $value;
          }
      }
  }

// affiche la saison
//echo saison();



function check_dimanche($value) {
preg_match(' /([0-9]+)\/([0-9]+)\/([0-9]+)/ ', $value , $match );
$date = date("l", mktime(0, 0, 0, $match[2], $match[1], $match[3]));
$date = trim($date);
if (strstr($date,"Sunday")) return 1;
else return 0;
}

//$date = "16/11/2014";
//echo check_dimanche($date)."<br />\n";


function check_sunday($timestamp)
{
if( date("w", $timestamp ) == 0 )  return 1;
}

//$timestamp = strtotime('22-09-2008');
//check_sunday($timestamp);


//$tabDate = explode('-', $_POST['date']);
//$timestamp = mktime(0, 0, 0, $tabDate[1], $tabDate[2], $tabDate[0];

/*$debut = "22-07-2015";
$table = explode("-", $debut);
$jour = $table[0];
$mois = $table[1];
$an = $table[2];
$timestamp =  mktime(0,0,0,07, 22, 2015);
echo $timestamp;
echo '<br>';
$timestamp =  mktime(0,0,0,$mois, $jour, $an);
echo $timestamp;
echo '<br>';

/*$jour = date('d');
$mois = date('m');
$an = date('Y'); 
$heure = date('H'); 
$minute = date('i'); 
$seconde = date('s');*/

function trouver_jour_semaine($date)
{
	$joursem = array('dim', 'lun', 'mar', 'mer', 'jeu', 'ven', 'sam');
	list($jour, $mois, $annee) = explode('-', $date);
	$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
	//echo $timestamp;
	return($joursem[date("w",$timestamp)]);
}

//fonctionne aussi;
//$jour = date('w', $timestamp);
//echo '<br>';
//echo $jour;
//echo '<br>';

trouver_jour_semaine("22-07-2015");



function calcul_timestamp($date)
{ 
	list($jour, $mois, $annee) = explode('-', $date);
	$timestamp = mktime (0, 0, 0, $mois, $jour, $annee);
	return($timestamp);
}
	 
function compter_jours($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);
}

echo '<br>';
echo '<br>';
echo '<br>';
/******************************************************************/	
//Sam 6
//Dim 1
//Lun 2
//Mer 3
//Jeu 4
//Ven 5

$debut_saison = "15-03-2015";
$debut_location_semaine = "01-06-2015";
$fin_location_semaine = "01-09-2015";
$debut_location = "18-07-2015";
$fin_location = "25-07-2015";
$fin_saison = "15-11-2015";

$date_du_jour = date("d-m-Y");

$stamp_date_du_jour = calcul_timestamp($date_du_jour);
$stamp_debut_saison = calcul_timestamp($debut_saison);
$stamp_debut_location = calcul_timestamp($debut_location);
$stamp_fin_location = calcul_timestamp($fin_location);
$stamp_debut_location_semaine = calcul_timestamp($debut_location_semaine);
$stamp_fin_location_semaine =  calcul_timestamp($fin_location_semaine);
$stamp_fin_saison = calcul_timestamp($fin_saison);

$nb_jours = compter_jours($debut_location, $fin_location);
//echo "Durée du séjour : " .$nb_jours;

$jour_debut_location = date('w', $stamp_debut_location);
$jour_fin_location = date('w', $stamp_fin_location);

//echo '<br>'; 
//echo $jour_debut_location;
//echo '<br>'; 
//echo $jour_fin_location; 

$erreur_jour_debut_location = "";
$erreur_jour_fin_location = "";
$erreur_hors_plage = "";

/************************************************************/

	if($stamp_debut_location < $stamp_date_du_jour || $stamp_fin_location < $stamp_date_du_jour )
	{ $erreur_hors_plage = "Votre selection est postérieur à la date du jour !";
	}
	
	//verifier que l'on est pas hors saison ou bien que le plage est libre

	
if($nb_jours < 7)
{ 
	if($stamp_debut_location >= $stamp_debut_saison && $stamp_fin_location <= $stamp_debut_location_semaine )
	{ echo "Location sur plusieurs jours possible";
	}
	
	if($stamp_debut_location >= $stamp_fin_location_semaine && $stamp_fin_location <= $stamp_fin_saison )
	{ echo "Location sur plusieurs jours possible";
	}

	if($stamp_debut_location >= $stamp_debut_location_semaine && $stamp_fin_location <= $stamp_fin_location_semaine )
	{ echo "Location a la semaine au court de la période estivale !";
	  $erreur_nb_jour_location_semaine = "Location a la semaine au court de la période estivale !";
	}
	
	
}
	
if($nb_jours >= 7)
{ 
	if( $jour_debut_location !=6)
	{ $erreur_jour_debut_location = "Une location commence obligatoirement un Samedi !";
	}
	if( $jour_fin_location !=6)
	{ $erreur_jour_fin_location = "Une location se termine  obligatoirement un Samedi !";
	}
	
}


?>