Running time (Calculating dates) - Coming from the Databank

Asked

Viewed 58 times

-1

Someone could help me find what is wrong when calling the date of the database in the function, I am also attaching a print: On the left the actual value of the last access and on the right would be responsible for taking the date from the left and making the calculation of the elapsed time.

inserir a descrição da imagem aqui

<?php
include_once "conexao.php";
date_default_timezone_set('America/Sao_Paulo');  
//consultar no banco de dados
$result_usuario = "SELECT DATE_FORMAT(data_acesso, '%Y/%m/%d %H:%i:%s') as data_acesso FROM adms_ultimos_acessos WHERE adms_usuario_id = '44'  ";
$resultado_usuario = mysqli_query($conn, $result_usuario) ;
$registro = mysqli_fetch_assoc($resultado_usuario ); // <--- esta linha


//Verificar se encontrou resultado na tabela "usuarios"
if(mysqli_num_rows($resultado_usuario) > 0){
    ?>

    <?php  

    function tempo_corrido($time) {

    date_default_timezone_set('America/Sao_Paulo');     

     //$time = '2019/03/02 18:54:10';
     $time = $registro['data_acesso']; // <-- aqui está o registro
     $now = strtotime(date('Y/m/d H:i:s'));
     $time = strtotime($time);
     $diff = $now - $time;

     $seconds = $diff;
     $minutes = round($diff / 60);
     $hours = round($diff / 3600);
     $days = round($diff / 86400);
     $weeks = round($diff / 604800);
     $months = round($diff / 2419200);
     $years = round($diff / 29030400);

    if ($seconds <= 60) return"1 min atrás";
     else if ($minutes <= 60) return $minutes==1 ?'1 min atrás':$minutes.' min atrás';
     else if ($hours <= 24) return $hours==1 ?'1 hrs atrás':$hours.' hrs atrás';
     else if ($days <= 7) return $days==1 ?'1 dia atras':$days.' dias atrás';
     else if ($weeks <= 4) return $weeks==1 ?'1 semana atrás':$weeks.' semanas atrás';
     else if ($months <= 12) return $months == 1 ?'1 mês atrás':$months.' meses atrás';
     else return $years == 1 ? 'um ano atrás':$years.' anos atrás';
     }                                  

    ?>
        <table class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th>Data acesso</th>

                </tr>
            </thead>
            <tbody>
                <?php
                while($registro = mysqli_fetch_assoc($resultado_usuario)){
                    ?>
                    <tr>
                        <th><?php echo $registro['data_acesso']; ?></th>
                        <th><?php echo tempo_corrido("m/d/Y H:i:s");
                    //echo $registro = @$str_ano, @$str_mes, @$str_dia,' ', @$str_hora, @$str_min, @$str_seg,  ' atrás'; ?>



                    </tr>
                    <?php
                }?>
            </tbody>
        </table>
    <?php
    }else{
        echo "<div class='alert alert-danger' role='alert'>Nenhum usuário encontrado!</div>";
    }
?>

1 answer

1


Dude you’re passing the wrong value to the function, and inside it you’re taking the value wrong.

Change the part of the code that calls the function

tempo_corrido("m/d/Y H:i:s");

for

tempo_corrido($registro['data_acesso']);

That is, so you pass this to the function to calculate the date ($registry['data_access']), which is the one you want to calculate.

And within the function, remove that line

$time = $registro['data_acesso']; // <-- aqui está o registro

For the value of $team is already being taken as a function parameter.

So you will calculate the $team (value of your registration) with $now (the current date)

See if everything works ok and.

  • Perfect VALEUUU was that same :D

Browser other questions tagged

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