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

6

I'm writing the date and time in the database with 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 registered from the server, not the Brasilia. How to record Brasília time?

    
asked by anonymous 11.02.2015 / 17:27

2 answers

14

Just use date_default_timezone_set of php and set the php- time as the 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 location of the desired time zone (eg, 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());
    
11.02.2015 / 17:32
-1

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)); 
    
23.06.2016 / 15:08