How to change the date coming from the Html form as d-m-Y to the Y-m-d format with PHP?

-2

I'm having difficulty in PHP code to write the date to 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 if the user enters the format: Year / month / day (Ymd) of the opposite if you type day / month / year (dmY), the date is zeroed. p>     

asked by anonymous 27.06.2018 / 02:36

1 answer

0

First of all, I recommend that you read the php link handbook. You will avoid a lot of headaches by reading and learning a little about 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";
    }
?>
    
27.06.2018 / 02:52