Convert time obtained by the database from ("Y-m-d H:i:s"); to ("Y-m-d-H-i-s");

Asked

Viewed 70 times

0

In this case how I can convert without using the method

DATE_FORMAT(data_access, '%Y-%m-%d-%H-%i-%s') in SELECT

 <?php                                            
    $resul_ult_aces = "SELECT as data_acesso FROM adms_ultimos_acessos
    WHERE adms_usuario_id = '".$row_user['id']."'
    ORDER BY id DESC LIMIT 1";
    $resultado_ult_aces = mysqli_query($conn, $resul_ult_aces);
    $row_ult_aces = mysqli_fetch_assoc($resultado_ult_aces);

    $data_atual = date("Y-m-d H:i:s");
    $data_ult_aces = $row_ult_aces['data_acesso'];
    $ob_data_atual = new DateTime($data_atual);
    $ob_data_ult_aces = new DateTime($data_ult_aces); 

    $intervalo = $ob_data_atual->diff($ob_data_ult_aces);
    ?>
  • you want to edit $current date or $data_ult_aces?

  • both so that I can solve a mistake, indeed try to see if it is so.

1 answer

1

By default the PHP date function expects the parameters for Day, Month and Year to be separated by '-' and the parameters of Time, Minute and Seconds per ':'.

To convert to the format you want, you can convert the date obtained by the function to a string and use PHP’s str_replace function to replace the ':' by '-'.

But I believe these conversions will make it difficult to compare dates.

  • So I tried using str_replace but it didn’t work

  • Could be more specific about what went wrong?

Browser other questions tagged

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