What you need to do is update the configuration of Timezone1 of Mysql. This way you can and will have the time according to the country/region you want. Below I show some ways.
Change during connection
Mysqli:
<?php
$conn = new mysqli('localhost', 'user', 'password', 'database');
if ($conn->connect_error) die ("Check error");
$conn->query("SET GLOBAL time_zone = '-03:00';");
/* ou */
$conn->query("SET time_zone = '-03:00';");
PDO:
<?php
$conn = new PDO('mysql:dbname=database;host=localhost', 'user', 'password');
$conn->query("SET GLOBAL time_zone = '-03:00';");
/* ou */
$conn->query("SET time_zone = '-03:00';");
Filing cabinet my.cnf:
In this file, you can use the settings below
default-time-zone='-03:00'
In the PHP:
An alternative is to use the PHP to convert dates. There are some options like: timezone
and DateTime
, for example.
Example with Datetime:
<?php
$date = new DateTime();
$date->setTimezone(new DateTimeZone('America/Sao_Paulo'));
$conn->query("INSERT INTO table (`name`, `date`) VALUES ('Nome', '{$date->format('Y-m-d H:i:s')}') ");
Example: https://ideone.com/IicXqf
Example with date_default_timezone_set:
<?php
date_default_timezone_set('America/Sao_Paulo');
$date = date('Y-m-d H:i:s');
$conn->query("INSERT INTO table (`name`, `date`) VALUES ('Nome', '{$date}') ");
Example: https://ideone.com/PTC2Lx
Answer to the question at https://answall.com/questions/151716/fuso-hor%C3%A1rio-brasileiro-no-mysql e https://stackoverflow.com/questions/930900/how-do-i-set-the-time-zonof-mysql/19069310#19069310
– Lucas Augusto Coelho Lopes
I read but it did not solve my problem she keeps entering with +4 hours :(
– Wesley Roberto
it explains how to do this with settings in "my.cnf" and I will not have access for example if I use a shared server, in the local server even get more have to work in both.
– Wesley Roberto
@Wesleyroberto, when connecting to the database, just run the query
SET GLOBAL time_zone = '-3:00';
orSET time_zone = '-3:00';
– Valdeir Psr
I read about it more I use in my connection file? I didn’t get it right, it serves for inserting new data? has some example of how I insert this line that you passed , please.
– Wesley Roberto
This is to change on the server right?
– Wesley Roberto