Get the current date and time here from Brazil to enter in the database

Asked

Viewed 681 times

4

I have a table that records the login and logout of users on the system. However, when the login is done, the date and time stored are not being added according to the time zone of Brasilia.

Follow the code below

<?php

    setlocale(LC_ALL, "pt_BR", "pt_BR.utf-8", "portuguese");

    /* Função para realizar o controle de login de usuários */
    function pegarLogin($conexao, $user)
    {
        $hora = date('H:i:s');
        $dia = date("Y-m-d");

        $query_login = "INSERT INTO log(tipo_reg,horario,usuario,data) VALUES('LOGIN','$hora','$user','$dia')"; 
        $resultado_query_login =  mysqli_query($conexao, $query_login);

        if(!$resultado_query_login) /*Verifica se o resultado deu certo ou errado*/
        {
            /*Se deu erro, então exibe a mensagem do sql sobre o erro */
            die("Falha no registro do login: " . mysqli_error($conexao)); 
        }

?>

Table image

inserir a descrição da imagem aqui

NOTE: I entered these data today at 14:00 and little.

  • You could do this directly by the bank through a PROCEDURE using the function now(), already through PHP I have no idea.

  • How would you get ONLY the right "time" now, using the now() ?

  • setlocale only changes language settings (numerical formatting, string comparison when there are accents, etc). Maybe what you want is date_default_timezone_set (the result of date('H:i:s') is in agreement with Brazil? ). Also remembering that the Mysql config may be using another Timezone, so the cause of this difference may be there: https://answall.com/a/371071/112052

  • The result of date('H:i:s') is not in accordance with the Brazilian time zone.

  • Ah, to get only the current time in Mysql, there is the function CURTIME

  • 2

    So at first date_default_timezone_set('America/Sao_Paulo') must solve... or use CURTIME even

  • 1

    I was going to talk about converting using the STR_TO_DATE to pick up just the time, but it would be like a gambit with the CURTIME it’s much better

  • CURTIME() fell like a glove. Draw up an answer there for me to accept.

  • I knew CURDATE(). But this CURTIME() was great.

  • @hkotsubo elaborate a reply ai. CURTIME() served me.

Show 5 more comments

1 answer

1


The function setlocale nay changes the time zone settings. According to documentation, it can change the date and time formatting made by the function strftime, but not the date and time values themselves.

In fact, according to documentation of date, the setlocale is recommended to be used together with strftime, to format dates in other languages.

So, date will use the time zone that is configured in PHP, and the date and time values will follow the current day and time of this zone. What you could do is use date_default_timezone_set to change the PHP Timezone (spindle), and so on date return the correct values (in your case, it could be date_default_timezone_set('America/Sao_Paulo'), for example).

Another point that may be the cause of this difference is the Timezone configured in Mysql (as explained in more detail here).

Anyway, if you only want the current time, you don’t need to generate it in PHP. Just use the function directly CURTIME in your query.

Browser other questions tagged

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