How to set date(’d/m/Y H:i:s') with Brasilia time instead of server time

Asked

Viewed 42,711 times

9

I’m recording the date and time in the database with the script:

$sql = "INSERT INTO admin_users (user_name, user_password_hash, user_email, user_data)
                        VALUES('" . $user_name . "', '" . $user_password_hash . "', '" . $user_email . "', '" . date('d/m/Y H:i:s') . "' );";

but the time is recorded from the server, and not from Brasilia. How to record the time of Brasilia?

  • What, just fix the server time? Or I who did not understand the question..

  • 1

    @Renan, if the server is in the same GMT range of Brasilia, have no reason to correct the time, but if not, the correction is necessary, can make use of time zones.

  • I’m on the local wamp server, when hosting I’ll have time issues?

  • 1

    It depends on where he is. If he is in São Paulo, for example, he can leave the time of Brasilia even.

  • 1

    Could this be? http://answall.com/a/47694/101?

2 answers

27


Just use the date_default_timezone_set php and set the time zone as Brasilia time, in this case 'America/Sao_paulo'. Here you can refer to the list of Supported Timezones.

NOTE: If your server is in the desired time zone location (e.g., São Paulo) you do not need to use date_default_timezone_set.

// DEFINE O FUSO HORARIO COMO O HORARIO DE BRASILIA
    date_default_timezone_set('America/Sao_Paulo');
// CRIA UMA VARIAVEL E ARMAZENA A HORA ATUAL DO FUSO-HORÀRIO DEFINIDO (BRASÍLIA)
    $dataLocal = date('d/m/Y H:i:s', time());
  • 3

    Or you can also use Datetime. $date = new Datetime("now", new Datetimezone("America/Sao_paulo"));

0

Follow the code I use.

$h = "3"; //HORAS DO FUSO ((BRASÍLIA = -3) COLOCA-SE SEM O SINAL -).
$hm = $h * 60;
$ms = $hm * 60;
//COLOCA-SE O SINAL DO FUSO ((BRASÍLIA = -3) SINAL -) ANTES DO ($ms). DATA
$gmdata = gmdate("m/d/Y", time()-($ms)); 
//COLOCA-SE O SINAL DO FUSO ((BRASÍLIA = -3) SINAL -) ANTES DO ($ms). HORA
$gmhora = gmdate("g:i", time()-($ms)); 

Browser other questions tagged

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