date conversion with php [duplicate]

2

Galera Today I use a very simple way to convert BD (American Standard) dates to PT-BR.

The way I use it like this:

$data_BD = "2016-07-27";

// Cria nome das variaveis
$ano = substr($data_BD,0,-6);
$mes = substr($data_BD,5,-3);
$dia = substr($data_BD,8);


$data = "$dia/$mes/$ano";

Well my question is, does PHP have any native function to perform this formatting? Because I have to convert both the DB dates to the user and those that the user informs input to save in DB.

    
asked by anonymous 27.07.2016 / 19:49

1 answer

3

You can use the function strtotime and date :

$data_BD = "2016-07-27";
$data = date("d/m/Y", strtotime($data_BD));

echo $data; // 27/07/2016

View demonstração

If you prefer to use DateTime :

$data_BD = "2016-07-27";

$dt = DateTime::createFromFormat('Y-m-d', $data_BD);
$data = $dt->format('d/m/Y');

echo $data; // 27/07/2016

See demonstração

    
27.07.2016 / 19:53