Highlight a time with PHP that is stored in the database

2

I'm creating a small table that has 7 columns, the first row of each column represents one day and the remaining times are that day's schedule.

I want the times that are stored in the database to be highlighted in this table. I tried in some ways, I did not succeed and I would like some help.

Here is the code, and I apologize for any basic error, I'm still not very familiar with PHP.

<table border="1">      
<?php
    $query = array();
    echo '<tr>';
    for ($i = 1; $i <= 7; $i++) {
        $data = date('d-m-Y',strtotime('+' . $i . ' days'));
        $query[$i] = mysql_query ("SELECT hora_consulta FROM consulta WHERE data_consulta =  '$data'");
        echo '<td>' . $data . '</td>';
    }
    echo '</tr>';

    $horario = '08:00';

    while (strtotime($horario) < strtotime('20:00')) {
        echo '<tr>';
        echo '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' ;

        $horario = strtotime("$horario + 30 minutes");
        $horario = date("H:i",$horario);
        echo '</tr>';
    }

?>
</table>
    
asked by anonymous 15.04.2014 / 22:21

2 answers

1

I think this is what you want:     

/*
* Inseri datas e horas no banco (apaguei algumas para simulação)

for ($i = 0; $i < 10; $i++) {
    $data = date("Y-m-d H:i:s", strtotime("today + $i days 08:00:00"));
    for ($a = 0; $a <= 8; $a++) {
    $data = date("Y-m-d H:i:s", strtotime("$data + 1 hours"));
    mysql_query("INSERT INTO dias_horas (data_hora) VALUES ('$data')") or die(mysql_error());
}
}*/

    echo "<table border=\"1\">";

$data_inicial = $data_loop = date("Y-m-d"); //Data de hoje 
$data_final = date("Y-m-d", strtotime("$data_inicial + 10 days"));

$data_aux = array();//Array que armazenará as datas e horas do banco de dados

$busca = mysql_query("SELECT data_hora FROM dias_horas ORDER BY data_hora");
while ($data = mysql_fetch_assoc($busca)) {
    $data_aux[] = date("Y-m-d H:i:s", strtotime($data["data_hora"]));
}


echo "<tr>";
while ($data_loop <= $data_final) { //O loop para o intervalo de dias
    echo "<th>" . date("d/m/Y", strtotime($data_loop)) . "</th>";
    $data_loop = date("Y-m-d", strtotime($data_loop . " + 1 days"));
}
echo "</tr>";


for ($i = 0; $i < 24; $i++) { //Loop para as horas do dia
    $data_loop = $data_inicial;
    echo "<tr>"; //Cada hora é em uma linha
    while ($data_loop <= $data_final) {
        echo "<td";
        /*Se houver aquela data e hora que o loop está percorrendo
        dentro do array, imprime o estilo negrito
        */
        echo in_array(date("Y-m-d H:i:s", strtotime($data_loop . " $i:00:00")), $data_aux) ? " style='font-weight:bold'" : "";
        echo ">$i:00</td>";
    $data_loop = date("Y-m-d", strtotime($data_loop . " + 1 days"));
    }
    echo "</tr>";
}

echo "</table>";
?>'
    
16.04.2014 / 03:59
0

Hello. I understood. Do you want to highlight your schedule with a different color? If so. You can add tag html , example: <red> or style color to css

echo "<td>".$horario."</td>
      <td><red>".$horario."</red></td>
      <td style='color:#FFFFFF'>".$horario."</td>
      <td><s>".$horario."</s></td>
      <td><b>".$horario."</b></td>
      <td><red>".$horario."</red></td>
      <td><blue>".$horario."</blue></td>" ;
    
16.04.2014 / 03:37