How to change the date that comes from the HTML form as d-m-Y to the Y-m-d format with PHP?

Asked

Viewed 234 times

-2

I’m having trouble with PHP code to record the date in my database. I have this code,

$data = $_POST ["data"]; 
$data = date("Y-m-d",strtotime(str_replace('/','-',$data)));

$newslleter = "INSERT INTO newslleter (data) VALUES ('$data')";

However, the date is only recorded in the database in case the user dies within the Input format: Year/month/day (Y-m-d) of the opposite type day/month/year (d-m-Y) the date is reset.

1 answer

0

First of all, I recommend a good read in the php manual http://php.net/manual/en/index.php . You will avoid a lot of headache by reading and learning some of the functions there :D

<?php
    if (isset($_POST)) { //TEM ALGO NO POST?
        $data = $_POST["data"];

        //Explode transforma uma string num vetor, separando os elementos a partir do char específicado.
        $vetData = explode("-", $data);

        /*
            echo "<pre>".print_r($vetData)."</pre>";

            Exemplo da saída Array ( [0] => 27 [1] => 06 [2] => 2018 )
        */

        $data = $vetData[2]."-".$vetData[1]."-".$vetData[0];

        $newslleter = "INSERT INTO newslleter (data) VALUES ('".$data."')";
    } else {
        echo "Favor preencher a data antes de acessar a página";
    }
?>
  • Error the code you gave me, I need something to receive in the fomato Day/Month/Year and pass to the code I have --> $name = $_POST ["name"]; $email = $_POST ["email"]; $data = $_POST ["date"]; $data = date("Y-m-d",strtotime(str_replace('/','-',$data)); $newslleter = "INSERT INTO newslleter (name,email,date) VALUES ('$name','$email','$data')";

  • Right, it’s / or - friend?

  • What’s the mistake? Try to specify

  • The php code does not recognize the date that is placed within the input as day/month/year and passes the reset date to the database, it inserts the right date only when the input is the year/month/day format. Put the code does not present a specific error just do not pass the date in the first citei format, if you can help me

  • Try to change the type of the date field, probably you planned it as date, change to varchar.

Browser other questions tagged

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