Connect time counter with the datetime of the database

Asked

Viewed 84 times

2

Guys I’m with this script but I can not connect to the database at all, please help me??

is working perfectly this way, more precise pull the date from the database.

Is returning these errors:

Fatal error: Uncaught Error: Call to a Member Function format() on Boolean in C: xampp htdocs listar listar_usuario.php:12 Stack trace: #0 {main} thrown in C: xampp htdocs listar listar_usuario.php on line 12

What’s in the database --> 2019-02-25 12:47:27
How it should present --> 1 day, 2 hours, 8 minutes and 4 seconds ago

    <?php
include_once "conexao.php";

//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 ORDER BY id DESC;";
$resultado_usuario = mysqli_query($conn, $result_usuario) ;
$registro = mysqli_fetch_assoc($resultado_usuario ); // <--- esta linha
$adms_ultimos_acessos = preg_replace("(\:|\s)", "-", $registro['data_acesso']);
$adms_ultimos_acessos = str_replace(":", "-", $registro['data_acesso']);
$adms_ultimos_acessos = str_replace(" ", "-", $adms_ultimos_acessos);
$adms_ultimos_acessos = DateTime::createFromFormat('Y-m-d H:i:s', $registro['data_acesso']);
$adms_ultimos_acessos = $adms_ultimos_acessos->format('Y-m-d-H-i-s');

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

<?php                                   
$adms_ultimos_acessos = $registro ['data_acesso']; // <-- aqui está o registro
$databd2 = date('Y-m-d-H-i-s');
$data1   = explode('-', $adms_ultimos_acessos); 
$data2   = explode('-', $databd2);  
$ano     = $data2[0] - $data1[0]; 
$mes     = $data2[1] - $data1[1]; 
$dia     = $data2[2] - $data1[2];  
$hora    = $data2[3] - $data1[3]; 
$min     = $data2[4] - $data1[4]; 
$seg     = $data2[5] - $data1[5];                                   
// configuração data  
if ($mes < 0) {$ano--;  $mes = 12 + $mes; }  
if ($dia < 0) { $mes--;     $dia = 30 + $dia; }  
if ($ano > 0) { $str_ano = $ano . ' ano'; } 
if ($ano > 1) { $str_ano .= 's '; }  
if ($mes > 0) { @$str_mes .= $mes . ' mes'; }  
if ($mes > 1) {     
if ($ano > 0) { $str_ano .= ', ';   }   $str_mes .= 'es'; } 
if ($dia > 0) { $str_dia = $dia . ' dia'; }  
if ($dia > 1) {     
if ($mes > 0) { $str_mes .= ', ';   }   $str_dia .= 's'; }                                      
// configuração hora  
if ($min < 0) {$hora--; $min = 60 + $min; }  
if ($seg < 0) { $min--; $seg = 60 + $seg; }  
if ($hora > 0) { $str_hora = $hora . ' hora'; }  
if ($hora > 1) { @$str_hora .= 's'; }  
if ($min > 0) { @$str_min .= $min . ' minuto'; }  
if ($min > 1) {     
if ($hora > 0) { @$str_hora .= ', ';    }   @$str_min .= 's'; }  
if ($seg > 0) { $str_seg = $seg . ' seg'; }  
if ($seg > 1) {     
if ($min > 0) { $str_min .= ' e ';  }   $str_seg .= 's'; }                                      

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

            </tr>
        </thead>
        <tbody>
            <?php
            while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
                ?>
                <tr>
                    <th><?php echo $row_usuario['data_acesso']; ?></th>
                    <th><?php echo @$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>";
}
  • This is the complete code ? because it has nothing to do with the database connection.

  • Yes I edited, the connection I made but I can’t pull the data from there understand? I’m not getting to do the SELECT* part and insert in $databd1

  • From what I understand I need to take the value of the query and insert in $databd1 is not?

  • Yes, your select will return only this value to be worked on the date?

  • What date do you need to pull from the bank? Date of a record?

  • But what is the name of the date field? What is the name of the table?

  • this exactly will work similarly to the time the comment was added, only I can’t create that part of SELECT on the date of registration, if you have an example using that code of mine or have some other solution that works in a similar way, give me the link please, thank you from now on

  • You need to be clearer Wesley. It’s not giving to understand what you want.

  • table:search field:databd1

  • $register = mysqli_fetch_assoc($query); instead of $query it would be $resultado_usuario, this is one of the first errors

  • With the help of Andrei I modified some things I will post a new edition there

  • Hello there as it stood and the mistakes of the return.

  • The format of the date saved in the seat?

  • He is saved like this: 2019-02-25 12:47:27 datetime

  • I think the error is in the structure that he is getting the date

Show 10 more comments

1 answer

2


The problem is the format of the date that is coming from the bank, you are getting in format aaaa-mm-dd hh:mm:ss and is treating it as if it were in format aaaa-mm-dd-hh-mm-ss, you need to convert it, follow a few options for you to choose a

  1. In SQL:

SELECT DATE_FORMAT(data_acesso, "%Y-%m-%d-%H-%i-%s") as data_acesso FROM adms_ultimos_acessos ORDER BY id DESC
  1. In PHP with preg_replace:

$adms_ultimos_acessos = preg_replace("(\:|\s)", "-", $registro['data_acesso']);
  1. In PHP with str_replace:

$adms_ultimos_acessos = str_replace(":", "-", $registro['data_acesso']);
$adms_ultimos_acessos = str_replace(" ", "-", $adms_ultimos_acessos);
  1. In PHP with DateTime:

$adms_ultimos_acessos = DateTime::createFromFormat('Y-m-d H:i:s', $registro['data_acesso']);
$adms_ultimos_acessos = $adms_ultimos_acessos->format('Y-m-d-H-i-s');
  • Error inserting SELECT DATE_FORMAT(accessdata_, "%Y-%m-%d-%H-%i-%s") as data_access FROM adms_ultimos_accesses ORDER BY id DESC

  • ai insert lines: $adms_ultimos_accesses = Datetime::createFromFormat('Y-m-d H:i:s', $record['data_access']); $adms_ultimos_accesses = $adms_ultimos_accesses->format('Y-m-d-H-i-s'); Returns error: Fatal error: Uncaught Error: Call to a Member Function format() on Boolean in C: xampp htdocs listar listar_usuario.php:24 Stack trace: #0 {main} thrown in C: xampp htdocs listar listar_usuario.php on line 24

  • I put 4 options should use only one of them

  • I just came back to read again, it is formatting more is not pulling the database data

Browser other questions tagged

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