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
NOTE: I entered these data today at 14:00 and little.
You could do this directly by the bank through a
PROCEDURE
using the functionnow()
, already through PHP I have no idea.– Patrick Perdigão
How would you get ONLY the right "time" now, using the now() ?
– Gato de Schrödinger
setlocale
only changes language settings (numerical formatting, string comparison when there are accents, etc). Maybe what you want isdate_default_timezone_set
(the result ofdate('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– hkotsubo
The result of date('H:i:s') is not in accordance with the Brazilian time zone.
– Gato de Schrödinger
Ah, to get only the current time in Mysql, there is the function
CURTIME
– hkotsubo
So at first
date_default_timezone_set('America/Sao_Paulo')
must solve... or useCURTIME
even– hkotsubo
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 theCURTIME
it’s much better– Patrick Perdigão
CURTIME() fell like a glove. Draw up an answer there for me to accept.
– Gato de Schrödinger
I knew CURDATE(). But this CURTIME() was great.
– Gato de Schrödinger
@hkotsubo elaborate a reply ai. CURTIME() served me.
– Gato de Schrödinger