Working with dates from MySQL / PHP

0

Inside a select in the bank, I look for the expiration date of the product. I need it to be calculated by changing a status in the 'beat', 'critical', and expired 'response. I'm trying to do it this way below, but I'm kind of rusty.

                        <?php
                    if($tabela == 'controle_insumos') {
                        $sql =  mysql_query("SELECT controle_insumos.*, prod01.* FROM controle_insumos INNER JOIN prod01 ON controle_insumos.rwc = prod01.procod LIMIT 200 ");
                    while ($result = mysql_fetch_array($sql) )
                    {
                    echo "<tr>";
                    echo "<td>".$result['rwc']."</td>";
                    echo "<td>".$result['prodes']."</td>";
                    echo "<td>".$result['quantidade']."</td>";
                    echo "<td>".$result['pround']."</td>";
                    echo "<td>";
                    echo date('d/m/Y', strtotime($result['data_compra']));
                    echo "</td>";
                    echo "<td>";
                    echo date('d/m/Y', strtotime($result['fabricacao']));
                    echo "</td>";
                    echo "<td>";
                    echo date('d/m/Y', strtotime($result['validade']));
                    echo "</td>";
                     $data2 = new DateTime($result['validade']);
                     $data1 = new DateTime();
                     $intervalo = $data1->diff($data2);


                        if ($intervalo<0) {
                            echo "<td style='background: red;'>";   
                            echo $intervalo->format('%R%');
                            echo "</td>";
                            }
                        elseif ($intervalo<30)  {
                            echo "<td style='background: green;'>";
                            echo $intervalo->format('%R%');
                            echo "</td>";
                            }
                        elseif ($intervalo>90)  {
                            echo "<td style='background: blue;'>";
                            echo $intervalo->format('%R%');
                            echo "</td>";
                        }
                    echo "</tr>";
                        }
                    }
                    else                        
                    $sql =  mysql_query("SELECT $res_codigo as rwc, $res_nome as descricao, $res_un as unidade FROM $tabela order by RAND() LIMIT 200 ");
                if($sql== 0)
                        $sql =  mysql_query("SELECT $res_codigo as rwc, $res_nome as descricao, $res_un as unidade FROM $tabela order by RAND()");
                    while ($result = mysql_fetch_array($sql) )
                    {
                    echo "<tr>";
                    echo "<td>".$result['rwc']."</td>";
                    echo "<td>".$result['descricao']."</td>";
                    echo "<td>".$result['unidade']."</td>";
                    echo "<td><button class='btn btn-lg btn-primary btn-block' type='submit'>Comprar</button></td>";
                    echo "</tr>";
                    }
            ?>
    
asked by anonymous 23.02.2018 / 15:16

1 answer

-1

Have a key that is being closed too, would that be the problem?

try this:

<?php
$data2 = new DateTime($result['validade']);
$data1 = new DateTime();
$intervalo = $data1->diff($data2); 
echo "<table>";
echo "<tr>";
if ($intervalo->days<0) {
    echo "<td style='background: red;'>";   
    echo $intervalo->format('%R%a dias');
    echo "</td>";
}
elseif ($intervalo->days<30)  {
    echo "<td style='background: green;'>";
    echo $intervalo->format('%R%a dias');
    echo "</td>";
}
elseif ($intervalo->days>90)  {
    echo "<td style='background: blue;'>";
    echo $intervalo->format('%R%a dias');
    echo "</td>";
}
echo "</tr>";
echo "</table>";

? >

    
23.02.2018 / 15:32