Convert data in the format dd / mm / yyyy to yyyy-mm-dd

5

I'm getting two post in php from two dates in the format dd / mm / yyyy however to display the precise data convert from dd / mm / yyyy to yyyy-mm-dd

Code:

   if(isset($_POST)) // Se existir o array post, pq ele não retorna undefined index.
    {   

        $data1 = $_POST["data1"];
        $data2 = $_POST["data2"];

$busca = mysql_connect("$local","$usuario","$senha") or die("ERRO AO CONECTAR AO MYSQL, VERIFIQUE COM O ADMINISTRADOR" . mysql_error());
    mysql_select_db("$banco") or die("BASE DE DADOS INVÁLIDO");
mysql_select_db("$banco") or die("BASE DE DADOS INVÁLIDO");

    $pesquisa = mysql_query("SELECT sum(ProdValor) FROM vendas WHERE data BETWEEN '$data1' AND '$data2' AND StatusTransacao = 'Aprovado' ");

    while($sum = mysql_fetch_array($pesquisa)){
    $soma2 = $sum['sum(ProdValor)'];
    }

    echo '<b>Aprovado</b> ' . $soma2 . '<br>'; 

}

    
asked by anonymous 16.04.2015 / 20:46

3 answers

4

MYSQL - It can be done directly using the DATE_FORMAT () function like this:

DATE_FORMAT(suadatadd/mm/yyyy,'%d/%m/%Y') as 'DATA'

PHP - A function can be done:

public static function dateEmMysql($dateSql){
    $ano= substr($dateSql, 6);
    $mes= substr($dateSql, 3,-5);
    $dia= substr($dateSql, 0,-8);
    return $ano."-".$mes."-".$dia;
}
  

link

    
16.04.2015 / 20:50
5

If it is by php , you can use the date and strtotime functions to return the date in the desired format.

<?php

    $data = str_replace("/", "-", $_POST["data1"]);
    echo date('Y-m-d', strtotime($data));
    
16.04.2015 / 21:06
-1

12/15/2018 - > 2018-12-15

$data = implode('-', array_reverse(explode('/', "15/12/2018")));

2018-12-15 - > 12/15/2018

$data = implode('/', array_reverse(explode('-', "2018-12-15")));
    
15.12.2018 / 11:47