Inserting current date in php

Asked

Viewed 3,053 times

1

I am trying to enter the current date as the person registers but I did not want the user to inform this data but rather to enter it through some function with mysql, to save the date the provider was registered. But I tried some ways and I couldn’t, I looked here on the forum and I couldn’t so I created the question

<?php  
        if(isset($_POST['enviar_for'])){

                $id = $_POST['id'];
                $fornecedor = $_POST['fornecedor'];
                $observacoes = $_POST['observacoes'];
                $data_cadastro = date('Y-m-d', strtotime($_POST['data_cadastro']));

                $sql = "SELECT * FROM cadfor WHERE id_pessoa = '$id' "; 
                $resulta = $conn->query($sql);
                $row = $resulta->fetch_assoc();

                if ($resulta->num_rows > 0 ){
                    $result_endereco = "UPDATE cadfor SET tipo_endereco = '$tipo_endereco', cep = '$cep ', tipo_logr = '$tipo_logr', nome_logr = '$nome_logr', nume_logr = '$nume_logr', comp_logr = '$comp_logr', cidade = '$cidade', bairro = '$bairro', uf = '$uf' WHERE id_pessoa = '$id' ";
                } else {
                    $result_endereco = "INSERT INTO cadfor(id_pessoa, fornecedor, observacoes, data_cadastro) VALUES ('$id', '$fornecedor', '$observacoes', 'NOW()' )";
                }
                $resultado = mysqli_query($conn, $result_endereco);
                echo $result_endereco;
        }
  ?>
  • Remove the quotes from the function NOW().

3 answers

4

You can use the function date() php passing the format you want.

date('Y-m-d H:i:s');
  • this format was not, I will update the code

  • The formats are different, you have to see what you need: http://php.net/manual/en/function.date.php

  • i wanted to enter the current date without the user needing to inform, but I threw it inside my code and it did not work even the field being date and I stating that it is date in php

  • If you store the value returned from the date function in a variable, the user will not need to fill in. The date function takes the current date and time from your server.

3


The function NOW() Mysql returns the current date.

You can do it like this:

$result_endereco = "INSERT INTO cadfor(id_pessoa, fornecedor, observacoes, data_cadastro) VALUES ('$id', '$fornecedor', '$observacoes', NOW())";

To work, the column data_cadastro has to be DATE, DATETIME or TIMESTAMP.

  • this format was not, I will update the code, it is of type date same but it was not

  • I made a comment on your question, look there.

  • thanks worked, nor had I attacked the quotes

0

You can use this:

date('Y-m-d H:i:s');
  • this format was not, I will update the code

Browser other questions tagged

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