date conversion with php

Asked

Viewed 32 times

2

People Today I use a very simple way to convert dates from BD (American standard) to PT-BR.

The way I use it is 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 dates of the BD to the user and the ones that the user informs on input to save in the comic.

  • 1

    I think that answers: http://answall.com/q/21774/91

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

See 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

Browser other questions tagged

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