Dibujar calendario con PHP

Hola tyros!

Clic aquí para ver en C#

Ahora les mostraré como dibujar un calendario en PHP.

Así sería el resultado final:



Aquí les dejo el código:

<?php
date_default_timezone_set ( "America/Mexico_City" );
function DibujarMes($year, $month) {
 $cal = "";
 $totalDias = cal_days_in_month ( CAL_GREGORIAN, $month, $year );
 $fechaIni = $year . "-" . $month . "-01";
 $diaInicial = ObtenerNumeroDia ( $fechaIni );
 $diaSemana = 1;
 $cal .= "<table class='table table-bordered'>";
 $cal .= "<thead>" . 
   "<tr><th colspan='7' class='text-center'>" . ObtenerMes ( date ( 'm', strtotime ( $fechaIni ) ) ) . "</th></tr>" . 
   "<tr> " . "   <th class='text-center'>LUNES</th>" . 
   "   <th class='text-center'>MARTES</th>" . 
   "   <th class='text-center'>MIÉRCOLES</th>" . 
   "   <th class='text-center'>JUEVES</th>" . 
   "   <th class='text-center'>VIERNES</th>" . 
   "   <th class='text-center'>SÁBADO</th>" . 
   "   <th class='text-center'>DOMINGO</th>" . 
   "</tr>" . 
   "</thead>";
 $cal .= "<tbody>";
 $cal .= "<tr>";
 
 for($i = 1; $i < $diaInicial; $i ++) {
  $dias = $i - $diaInicial;
  $cal .= "<td class='cal-day-empty'><div class='cal-day'>" . date ( "d", strtotime ( $fechaIni . "+$dias  days" ) ) . "</div></td>";
  $diaSemana ++;
 }
 
 for($i = 0; $i <= $totalDias - 1; $i ++) {
  $cal .= "<td><div class='cal-day'>" . date ( "d", strtotime ( $fechaIni . "+$i  days" ) ) . "</div><br />Contenido</td>";
  if (date ( "l", strtotime ( $fechaIni . "+$i  days" ) ) == "Sunday") {
   $cal .= "</tr><tr>";
   $diaSemana = 1;
  }
  $diaSemana ++;
 }
 $dias = $totalDias - 1;
 if (date ( "l", strtotime ( $fechaIni . "+$dias  days" ) ) != "Sunday") {
  $dia = 0;
  for($j = $diaSemana - 1; $j <= 7; $j ++) {
   $dias = $totalDias + $dia;
   $cal .= "<td class='cal-day-empty'><div class='cal-day'>" . date ( "d", strtotime ( $fechaIni . "+$dias  days" ) ) . "</div></td>";
   // cal.Append(String.Format("<td class='cal-day-empty'><div class='cal-day'>{0:dd}</div></td>",fechaIni.AddDays(totalDias + dia)));
   $dia ++;
  }
 }
 
 $cal .= "</tr>";
 $cal .= "</tbody>";
 $cal .= "</table>";
 
 return $cal;
}
function ObtenerNumeroDia($fecha) {
 switch (date ( 'l', strtotime ( $fecha ) )) {
  case "Monday" :
   return 1;
  case "Tuesday" :
   return 2;
  case "Wednesday" :
   return 3;
  case "Thursday" :
   return 4;
  case "Friday" :
   return 5;
  case "Saturday" :
   return 6;
  case "Sunday" :
   return 7;
  default :
   return 0;
 }
 
 return date ( 'l', strtotime ( $fecha ) );
}
function ObtenerMes($mes) {
 $meses = array (
   "ENERO",
   "FEBRERO",
   "MARZO",
   "ABRIL",
   "MAYO",
   "JUNIO",
   "JULIO",
   "AGOSTO",
   "SEPTIEMBRE",
   "OCTUBRE",
   "NOVIEMBRE",
   "DICIEMBRE" 
 );
 if (($mes >= 1) && ($mes <= 12)) {
  return $meses [$mes - 1];
 } else {
  return "";
 }
}

?>
<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TyroDeveloper</title>
<link
 href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
 rel="stylesheet" />
<style type="text/css">
.cal-day-empty {
 background-color: #DDDDDD;
}

.cal-day {
 position: relative;
 float: left;
 top: 0px;
 left: 0px;
 display: block;
 height: 20px;
 width: 20px;
 padding: 0;
 margin: 0px;
 border-bottom: 1px solid silver;
 border-right: 1px solid silver;
}
</style>
</head>
<body>
 <form id="frmCalendario">
  <div class="container">
  <?php
  for($i = 1; $i <= 12; $i ++) {
   echo DibujarMes ( 2017, strlen ( $i ) == 1 ? "0" . $i : $i );
  }
  
  ?>
  </div>
 </form>
</body>
</html>

No hay comentarios:

Publicar un comentario