The following code corresponds to a For cycle that fills a calendar made with HTML (see the following image). In this case, the events obtained by dates from the DB are shown. Those dates are doubled and try to fix it with an if but it just stops me from repeating the date of the last event.
Conexion :: abrirConexion();
// Set your timezone
date_default_timezone_set('America/New_York');
// Get prev & next month
if (isset($_GET['ym'])) {
$ym = $_GET['ym'];
} else {
// This month
$ym = date('Y-m');
}
// Check format
$timestamp = strtotime($ym . '-01');
if ($timestamp === false) {
$ym = date('Y-m');
$timestamp = strtotime($ym . '-01');
}
// Today
$hoy = date('Y-m-j', time());
// For H3 title
$mes = date('F / Y', $timestamp);//F era m POR SI ALGO
// Create prev & next month link mktime(hour,minute,second,month,day,year)
$anterior = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp)));
$posterior = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp)));
// Number of days in the month
$contador_dias = date('t', $timestamp);
// 0:Sun 1:Mon 2:Tue ...
$texto = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp)));
//$str = date('w', $timestamp);
// Create Calendar!!
$semanas = array();
$semana = '';
// Add empty cell
$semana .= str_repeat('<td></td>', $texto);
// AQUI SE OBTIENEN LOS EVENTOS DESDE LA DB
$eventos = DAOEvento :: consultarTodos(Conexion :: obtenerConexion());
for ($dia = 1; $dia <= $contador_dias; $dia++, $texto++) {
$fecha = $ym . '-' . $dia;
if ($hoy == $fecha) {
$semana .= '<td class="hoy">' . '<a href="'.$ruta_elegida."?fecha=".$fecha.'&f=true">'.$dia.'</a>';
} else {
foreach ($eventos as $evento) {//AQUI SE VERIFICAN LAS FECHAS DEL CALENDARIO CON LAS ENCONTRADAS EN LA BD
$fecha_obtenida = $evento -> obtenerCierre();
list($fecha_cierre, $hora_cierre) = explode(' ', $fecha_obtenida);
$fecha_recuperada = $evento -> obtenerInicio();
list($fecha_inicio, $hora_inicio) = explode(' ', $fecha_recuperada);
if ($fecha_inicio == $fecha) {
$semana .= '<td class="evento">' . '<a href="'.$ruta_elegida."?fecha=".$fecha.'&f=true&e=true">'.$dia.'</a>';
}
}
if ($fecha !== $fecha_inicio) {//SIN ESTE IF LA FECHA 16 SE REPITE DOS VECES AL IGUAL QUE SE REPITE EL DIA 12 (EN ESTE CASO)
$semana .= '<td>' . '<a href="'.$ruta_elegida."?fecha=".$fecha.'&f=true">'.$dia.'</a>';
}
}
$semana .= '</td>';
// End of the week OR End of the month
if ($texto % 7 == 6 || $dia == $contador_dias) {
if ($dia == $contador_dias) {
// Add empty cell
$semana .= str_repeat('<td></td>', 6 - ($texto % 7));
}
$semanas[] = '<tr>' . $semana . '</tr>';
// Prepare for new week
$semana = '';
}
}
?>
<div class="container">
<h3><a href="?ym=<?php echo $anterior; ?>"><</a> <?php echo $mes; ?> <a href="?ym=<?php echo $posterior; ?>">></a></h3>
<table class="table table-bordered">
<tr>
<th>Dom</th>
<th>Lun</th>
<th>Mar</th>
<th>Mie</th>
<th>Jue</th>
<th>Vie</th>
<th>Sab</th>
</tr>
<?php
foreach ($semanas as $semana) {
echo $semana;
}
?>
</table>
</div>
It is worth mentioning that I tried to change the if of the "solution" for a while or switch, but it told me that the bytes were over or something like that.
I appreciate the contributions in advance to solve the problem.