Calendrier

Description

Cette classe affiche un calendrier et met une date sélectionné en surbrillance.
N'ayant pas trouvé mon bonheur (peut-être n'ai-je pas assez cherché), j'ai décidé de réinventer la roue.
Cette source affiche un calendrier par l'intermédiaire d'un appel de classe de la manière suivante:
A l"endroit ou vous voulez que le calendrier s'affiche, écrivez

$calendrier = new agenda;
$calendrier->affichage();

Source / Exemple :


<?php

class agenda
{ // BEGIN class agenda
	// variables
  var $jour=NULL;
  var $mois=NULL;
  var $annee=NULL;

	// constructor
	function agenda()
	{ // BEGIN constructor
  	global $_GET;
    setlocale(LC_TIME, 'fr_Fr', 'french');
		// $date au format jj-mm-yyyy
  	if (isset($_GET['date']))
    {
   	  $tableau=explode('-',htmlentities($_GET['date']));
      $this->jour=trim($tableau[0]);
   	  $this->mois=trim($tableau[1]);
      $this->annee=trim($tableau[2]);
   	  //  on va faire quelques vérifications
   	  if (!is_numeric($this->annee) or strlen($this->annee)!=4) { $this->annee=date("Y"); }
   	  if (!is_numeric($this->mois) or strlen($this->mois)>2 or $this->mois>12) { $this->mois=date("m"); }
      if (!is_numeric($this->jour) or strlen($this->jour)>2 or $this->jour>date("t",mktime(0,0,0,$this->mois,1,$this->annee))) { $this->jour=1; }
    }
    else
    {
   	  $this->annee=date("Y");
   	  $this->mois=date("m");
   	  $this->jour=date("d");
    }
	} // END constructor
	//**************************************************************************//
	function affichage()
	{ // BEGIN affichage
    
    setlocale(LC_TIME, 'fr_Fr', 'french');
    // nb de jours dans le mois
    $jour_mois=date("t",mktime(0,0,0,$this->mois,$this->jour,$this->annee));
    // numéro du premier jour du mois
    $prem_jour=date("w",mktime(0,0,0,$this->mois,1,$this->annee));
    // calcul du nombre de lignes necessaires
    //  on compte le nb de cases rempli sur la première ligne
    if ($prem_jour!=0) { $prem_case=$prem_jour-1; } else { $prem_case=6; }
    // on calcule le numéro de la semaine du premier jour du mois
    $nuro_p=date("W", mktime(0,0,0,$this->mois,1,$this->annee));
    // feinte parce que le jour de la semaine peut appartenir à la semaine de l'année précédente
    // et ça met le bigntz pour les années 2010, 2011 et 2012
    if ($nuro_p>=52) { $nuro_p=0; }
    // on  calcule le numéro de la semaine du dernier jour du mois
    $nuro_d=date("W", mktime(0,0,0,$this->mois,$jour_mois,$this->annee));
    // nombre de ligne
    $nb_ligne=$nuro_d-$nuro_p;
    ?>
    <table width="490" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#330066" bgcolor="#FFFFFF">
      <tr>
        <td colspan="2">
          <h3><?php echo '<a href="index.php?date=01-01-'.($this->annee-1).'">'.($this->annee-1).'</a>'; ?></h3>
        </td>
        <td colspan="3">
          <h1><?php echo $this->annee; ?></h1>
        </td>
        <td colspan="2">
          <h3><?php echo '<a href="index.php?date=01-01-'.($this->annee+1).'">'.($this->annee+1).'</a>'; ?></h3>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <h3><?php echo '<a href="index.php?date='.date("d-m-Y",mktime(0, 0, 0, $this->mois-1, 1, $this->annee)).'">'.htmlentities(strftime("%B %Y",mktime(0, 0, 0, $this->mois-1, 1, $this->annee))).'</a>'; ?></h3>
        </td>
        <td colspan="3">
          <h2><?php echo htmlentities(strftime("%A %e %B %Y",mktime(0,0,0,$this->mois,$this->jour,$this->annee))); ?></h2>
        </td>
        <td colspan="2">
          <h3><?php echo '<a href="index.php?date='.date("d-m-Y",mktime(0, 0, 0, $this->mois+1, 1, $this->annee)).'">'.htmlentities(strftime("%B %Y",mktime(0, 0, 0, $this->mois+1, 1, $this->annee))).'</a>'; ?></h3>
        </td>
      </tr>
      <tr bgcolor="#99CCFF">
        <td width="70" height="30" align="center">L</td>
        <td width="70" height="30" align="center">M</td>
        <td width="70" height="30" align="center">M</td>
        <td width="70" height="30" align="center">J</td>
        <td width="70" height="30" align="center">V</td>
        <td width="70" height="30" align="center">S</td>
        <td width="70" height="30" align="center">D</td>
      </tr>
      <tr>
    <?php
    $compte_jour=1;
    for ($i=0;$i<=$nb_ligne;$i++)
    {
      ?>
    	<tr>
    	<?php
      for ($j=0;$j<=6;$j++)
      {
        if (($i==0 and $j<$prem_case) or ($i==$nb_ligne and $compte_jour>$jour_mois))
        {
          // Si on est hors du décompte
          ?>
        <td>
          &nbsp;
        </td>
          <?php
        }
        elseif (($i==0 and $j>=$prem_case) or ($i==$nb_ligne and $compte_jour<=$jour_mois))
        {
          $chaine=$compte_jour.'-'.$this->mois.'-'.$this->annee;
          $url=urlencode($chaine);
          // si on est dans la première ligne ou la dernière est qu'on est dans le décompte
          $class='';
          if ($this->jour==$compte_jour)
          {
          	$class=' style="background-color:#99CCFF;"';
          }
          ?>
        <td align="center" <?php echo $class; ?>>
          <?php
          echo '<a href="index.php?date='.$url.'">'.$compte_jour.'</a>';
          ?>
        </td>
          <?php
          $compte_jour++;
        }
        else
        {
          $chaine=$compte_jour.'-'.$this->mois.'-'.$this->annee;
          $url=urlencode($chaine);
          // si on est ni en première, ni en dernière ligne, donc dans le décompte 
          $class='';
          if ($this->jour==$compte_jour)
          {
          	$class=' style="background-color:#99CCFF;"';
          }
          ?>
        <td align="center" <?php echo $class; ?>>
          <?php
          echo '<a href="index.php?date='.$url.'">'.$compte_jour.'</a>';
          ?>
        </td>
          <?php
          $compte_jour++;
        }
      }
      ?>
    	</tr>
    	<?php
    }
    ?>
    </table>
    <?php
	} // END affichage
} // END class agenda

?>

Conclusion :


L'affichage est pas super top, mais je n'ai pas trouvé mieux pour l'instant.
Appel de la classe en faisant:

$calendrier = new agenda;
$calendrier->affichage();

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.