How to insert data into Msqli database using current date

Asked

Viewed 73 times

0

How I can enter the data into the database using the current date of Brazil and not the hour of USA?

Code:

     $result_cad_user = "INSERT INTO adms_usuarios (nome, email, usuario, senha, $campo_foto adms_niveis_acesso_id, adms_sits_usuario_id, created  ) VALUES (
    '" . $dados_validos['nome'] . "',
    '" . $dados_validos['email'] . "',
    '" . $dados_validos['usuario'] . "',
    '" . $dados_validos['senha'] . "',
    $valor_foto
    '" . $dados_validos['adms_niveis_acesso_id'] . "',
    '" . $dados_validos['adms_sits_usuario_id'] . "',
    NOW())";
  • 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

  • I read but it did not solve my problem she keeps entering with +4 hours :(

  • 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.

  • @Wesleyroberto, when connecting to the database, just run the query SET GLOBAL time_zone = '-3:00'; or SET time_zone = '-3:00';

  • 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.

  • This is to change on the server right?

Show 1 more comment

2 answers

1

Hi, I enter the creation date in my tables as follows:

.date('Y-m-d').

And when I want to display the date I use:

   $data_formatada = date("d/m/Y", strtotime($data_cadastro));

// Modifies the time zone to be used. Available since PHP 5.1

date_default_timezone_set('UTC');

You can understand better here

  • My problems are in the registration of new users, when I will insert them they are always inserted with +4 hours and I want it to insert in the Brazilian schedule (SP) ie -4 hours that the server . I wonder if you can help me?

  • @Wesleyroberto I edited my answer from a look see if it helps you, if not I try something here.

  • i could not with these date_default_timezone_set('UTC'); just to know if I am right I use at the beginning of the code responsible for processing the data to the database is not?

  • at the beginning of your code use this here <?php echo date_default_timezone_get(); ? > so we can see which zone it’s picking up

  • managed using $Conn->query("SET time_zone = '-03:00';"); and date_default_timezone_set('America/Sao_paulo'); Thank you very much, something else on time display it is displaying like this: 0 days, 0 hours, 2 minutes, It has as I hide the minutes after I complete 59 and display only the hours ?

1

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

  • I think I’d better carry the Timezone Tables and use the Timezone America/Sao_Paulo. If you use a fixed value such as -03:00, you will have to change again in daylight saving time (during daylight saving time it becomes -02:00), and when daylight saving time ends, change again to -03:00. Already using America/Sao_Paulo (and with Timezone Tables configured), these changes are automatically managed

  • 1

    @hkotsubo, true, just as he could also use the option --timezone when starting the service. However, the user may not have access while using a shared server, according to his comment https://answall.com/questions/364417/comort-insertdatas-no-banco-msqli-usando-data-actual/364442#comment727464_364417

  • Is there any other way to do it ?? if you help me.. Thanks

  • @Wesleyroberto That’s what?

Browser other questions tagged

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