Inserting datetime into SQLSRV using PHP

Asked

Viewed 933 times

0

I am working with PHP and SQLSRV.

I need to enter a date in a field of the type Datetime and I can not at all. At least not in the ways I have found in Internet searches.

The last one I tried was this, because they commented that it worked:

if(isset($_POST['cadastrar'])){ 

    $dataInicio = $_POST['data'];

    $dataHoje = date("Y-m-d");

    $dataInicio['data'] = $_POST['data'];
    $data = DateTime::createFromFormat('l, j F, Y', $dataInicio['data']);
    $data = $data->format('Y-m-d H:i:s');

    $dataHoje['data'] = $_POST['data']; 
    $dataHj = DateTime::createFromFormat('l, j F, Y', $dataHoje['data']);
    $dataHj = $dataHj->format('Y-m-d H:i:s');

    $sql = "INSERT INTO [RDO].[dbo].[CAD_FUN] (DATA, DATAINI) VALUES (?,?)";

    $params = array($data, $data);

However, when clicking on register, the following warning appears:

Fatal error: Call to a Member Function format() on a non-object

  • It would be easier to look at the code that handles the INSERT command. But basically it is better to use parameters in your SQL command and pass the date as Datetime even instead of formatting it as string. If you prefer string, the universal MS SQL Server format for data entry is yyyy-MM-dd hh:mm:ss.

  • 2

    Ah, as for your mistake, it seems $data cannot receive a DateTime. In this case, $_POST['data'] may be empty or contain invalid data. You can debug?

  • Caffé! Follow my code: if(isset($_POST['register']){ $dataInicio = $_POST['data']; $dataHoje = date("Y-m-d"); $dataInicio['data'] = $_POST['data']; $data = Datetime::createFromFormat('l, j F, Y', $dataInicio['data']); $data = $data->format('Y-m-d H:i:s'); $dataHoje['data'] = $_POST['data']; $dataHj = Datetime::createFromFormat('l, j F, Y', $dataHoje['data']); $dataHj = $dataHj->format('Y-m-d H:i:s'); $sql = "INSERT INTO [RDO]. [dbo]. [CAD_FUN] (DATA, DATAINI) VALUES (?,? )"; $params = array($date, $date);

  • Cool, Gustavo. But you need to edit your question and put the code there instead of in the comments.

  • Ready Coffee, I put up the code. I hope it helps.

1 answer

1


By your error message, you must be getting nothing or an invalid value for Datetime on the line $dataInicio = $_POST['data'];. First of all you need to fix this.

Once I can create a DateTime successfully, that is, after your line $data = DateTime::createFromFormat('l, j F, Y', $dataInicio['data']); function, simply delete the subsequent date formatting and pass the DateTime as a parameter for the query instead of passing a formatted string.

Also note that your code is overwriting the value of variables that it has just set itself and is setting variables that it will not use.

Basically I removed from your code the extra lines that are disturbing or are useless:

if(isset($_POST['cadastrar'])){
        # $dataInicio  = $_POST['data'];
        # $dataHoje = date("Y-m-d");
        # Opção para criar DateTime a partir da data entrada no formulário:
        $data = new DateTime($_POST['data']);
        # $data = $data->format('Y-m-d H:i:s');
        # $dataHoje['data'] = $_POST['data'];
        # $dataHj = DateTime::createFromFormat('l, j F, Y', $dataHoje['data']);
        # $dataHj = $dataHj->format('Y-m-d H:i:s');
        $sql = "INSERT INTO [RDO].[dbo].[CAD_FUN] (DATA, DATAINI) VALUES (?,?)";
        $params = array($data, $data);
  • Okay, Coffee, part I get. I took the lines you told me to take. Now, I did not understand the part that you say to remove or said that should be invalid value coming from $dataInicio = $_POST['data'], because this value I am picking from the form, when the user will select a calendar date. And how to do it, too, to get the date of the day? Because php is date("Y-m-d").

  • Managed to insert records with date? To get today’s date you can do $hoje = new DateTime();.

  • Dude, give an echo and look at what’s coming in the variables: dataInicio 2014-10-01 dataHoje 2014-09-30

  • I’ll put this new Datetime();

  • I put $data = new Datetime(); and then I echo to see what was coming and the following warning appeared: Catchable fatal error: Object of class Datetime could not be converted to string

  • Good morning Coffee! Any more tips?

  • 1

    Got it. What must be done to get the date the user enters the form is $data = new Datetime($_POST['data']).

  • @Gustavosevero Good, Gustavo! I wasn’t around and I only saw your messages now. I updated your code to Datetime in response to stay as a reference if anyone else comes by.

  • Caffé, something strange is happening. I am putting Datetime ($_POST['data']), in another form and the date enters the field with the date "1905-06-06". Do you know why this might be happening?

  • It’s working again. Thanks.

Show 5 more comments

Browser other questions tagged

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