Working with Mysql/PHP dates

Asked

Viewed 81 times

0

Within a select in the bank, I look for the expiry date of the product. I need it calculated by changing a status in the 'winning', 'critical' and beaten' response. I’m trying to do it this way below, but I’m a little 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>";
                    }
            ?>

1 answer

-1


There is a key that is being closed too much, that would 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>";

?>

  • In fact, the code does not end there. It remains whole

  • then what would be your doubt? what is not working?

  • It worked! Sorry! thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.