Ver Mensaje Individual
  #6 (permalink)  
Antiguo 17/04/2013, 15:23
Avatar de HiToGoRoShi
HiToGoRoShi
 
Fecha de Ingreso: abril-2008
Mensajes: 849
Antigüedad: 16 años
Puntos: 31
Respuesta: Aporte de calendario para eventos

Ya esta modificado, vuelvo adjuntar todo el código en un solo archivo para que lo puedan usar ..

Al ingresar una fecha al calendario solo ingresar el año y el mes, el formato debe ser este 2013-04 Y-m

Código PHP:
Ver original
  1. <?php
  2. class Calendario
  3. {
  4.     private $fecha;
  5.     private $mes;
  6.     private $hoy;
  7.     private $w;
  8.     private $eventos = array();
  9.    
  10.     public function __CONSTRUCT($fecha = null)
  11.     {
  12.         // La fecha ingresada
  13.         $this->fecha = new DateTime($fecha);
  14.         // La cantidad de dias del mes
  15.         $this->mes = $this->fecha->format('t');
  16.         // La fecha de hoy
  17.         $this->hoy = new DateTime(date('ymd'));
  18.         // El valor numerico de la semana del primer dia del mes
  19.         $this->w = $this->fecha->format('w');
  20.         // Restamos para saber desde donde va a empezar a pintar, por ejemplo
  21.         // si comienza un miercoles, debe dejar 3 cuadros vacios atraz
  22.         $this->w = $this->w > 0 ? -$this->w + 1 : 0;
  23.     }
  24.     public function setEvento(Evento $evento)
  25.     {
  26.         $this->eventos[] = $evento;
  27.     }
  28.     public function Pintar()
  29.     {
  30.         // Clonamos el objeto DateTime para ser modificado
  31.         $fecha_navegacion = clone $this->fecha;
  32.         // Un mes atraz
  33.         echo '<a href="?f=' . $fecha_navegacion->modify('-1 month')->format('Y-m') . '">' . $fecha_navegacion->format('Y F') . '</a> - ';
  34.         // Un mes adelante  
  35.         echo '<a href="?f=' . $fecha_navegacion->modify('+2 month')->format('Y-m') . '">' . $fecha_navegacion->format('Y F') . '</a>';
  36.        
  37.         // Armamos el calendario
  38.         echo '<table class="calendario">';
  39.         echo '
  40.             <tr>
  41.                 <th class="mes" colspan="7">' . ($this->fecha->format('F')) . '</th>
  42.             </tr>
  43.             <tr>
  44.                 <th>Domingo</th>
  45.                 <th>Lunes</th>
  46.                 <th>Martes</th>
  47.                 <th>Miercoles</th>
  48.                 <th>Jueves</th>
  49.                 <th>Viernes</th>
  50.                 <th>Sabado</th>
  51.             </tr>
  52.         ';
  53.         // Con esto sabemos cuando hacer un TR
  54.         $i=0;
  55.         for($this->w;$this->w<=$this->mes;$this->w++)
  56.         {
  57.             echo ($i == 0) ? '<tr>' : '';
  58.             echo ($i%7 == 0 && $i > 0) ? '</tr><tr>' : '';
  59.            
  60.             if($this->w<=0)
  61.             {
  62.                 echo '<td></td>';
  63.             }else
  64.             {
  65.                 echo '<td ' . ($this->hoy->format('ymj') == $this->fecha->format('ym') . $this->w ? 'class="hoy"' : '') . '>'; // Si el dia es hoy
  66.                 // Pintamos el dia actual
  67.                 echo '<span class="dia">' . $this->w . '</span>';
  68.                 // Agregar los eventos
  69.                 foreach($this->eventos as $e)
  70.                 {
  71.                     if($e->getFecha()->format('ymj') == $this->fecha->format('ym') . $this->w)
  72.                     {
  73.                         echo '<div class="evento">';
  74.                         echo '- <a href="#" class="asunto">' . $e->getAsunto() .  '</a> ';
  75.                         echo 'programado para las ' . $e->getHora() . ' por <b>' . $e->getCreador() . '</b>';
  76.                         echo '</div>';                 
  77.                     }
  78.                 }
  79.                 echo '</td>';
  80.             }
  81.             $i++;
  82.         }
  83.         echo '</table>';
  84.     }
  85. }
  86.  
  87. class Evento
  88. {
  89.     private $fecha;
  90.     private $hora;
  91.     private $creador;
  92.     private $asunto;
  93.    
  94.     public function getFecha() { return $this->fecha; }
  95.     public function getHora() { return $this->hora; }
  96.     public function getCreador() { return $this->creador; }
  97.     public function getAsunto() { return $this->asunto; }
  98.     public function setFecha($x)
  99.     {
  100.         $this->fecha = new DateTime($x);
  101.         $this->hora = $this->fecha->format('h:ia');
  102.     }
  103.     public function setCreador($x) { $this->creador = $x; }
  104.     public function setAsunto($x) { $this->asunto = $x; }
  105. }
  106.  
  107. // Instanciamos el calendario
  108. $c = new Calendario(isset($_GET['f']) ? $_GET['f'] : date('Y-m'));
  109.  
  110. // Seteamos un evento
  111. $e = new Evento();
  112. $e->setFecha('2013-04-16 14:02:00');
  113. $e->setCreador('HiToGoRoShi');
  114. $e->setAsunto('Reunion en con nuestro cliente');
  115.  
  116. $c->setEvento($e);
  117.  
  118. // Seteamos otro evento
  119. $e = new Evento();
  120. $e->setFecha('2013-04-18 14:07:00');
  121. $e->setCreador('Kenshin');
  122. $e->setAsunto('Capacitacion en sistema');
  123.  
  124. $c->setEvento($e);
  125.  
  126. // Seteamos otro evento
  127. $e = new Evento();
  128. $e->setFecha('2013-04-18 14:15:00');
  129. $e->setCreador('Raul');
  130. $e->setAsunto('Reunion con los proveedores');
  131.  
  132. $c->setEvento($e);
  133.  
  134. // Seteamos otro evento
  135. $e = new Evento();
  136. $e->setFecha('2013-04-24 14:16:00');
  137. $e->setCreador('Pepe');
  138. $e->setAsunto('Cena con los empleados');
  139.  
  140. $c->setEvento($e);
  141.  
  142. // Seteamos otro evento
  143. $e = new Evento();
  144. $e->setFecha('2013-05-17 14:16:00');
  145. $e->setCreador('Pepe');
  146. $e->setAsunto('Cena con los empleados');
  147.  
  148. $c->setEvento($e);
  149.  
  150. ?>
  151.  
  152. <!DOCTYPE html>
  153. <html>
  154.     <head>
  155.         <title>Calendario de eventos creado por HiToGoRoShi</title>
  156.         <link href="calendario.css" rel="stylesheet" />
  157.     </head>
  158. <body>
  159.     <h1>Calendario de eventos creado por HiToGoRoShi</h1>
  160.     <?php $c->Pintar(); ?>
  161. </body>
  162. </html>

El ejemplo en linea lo vemos acá
http://kimsa-media.com/calendario/

Me gustaría poder modificar el primer tema para no traer confusiones