Converting PHP Data to pt_br format

Asked

Viewed 80 times

0

How I can convert the date of 2019-02-22 for the Brazilian format 22/02/2019?

$data_atual = date('d-m-Y H:i:s');

date_default_timezone_set('America/Sao_Paulo');    
$data_ult_aces = $row_ult_aces['data_acesso'];
$ob_data_atual = new DateTime($data_atual);
$ob_data_ult_aces = new DateTime($data_ult_aces); 

I also tried to use the date_format('d/m/y'); didn’t work.

  • 1

    You might help: https://answall.com/a/348155/99718 and https://answall.com/q/109985/99718 and https://answall.com/a/338494/99718 and https://answall.com/a/358583/99718

  • do not need to put the (Solved) in the title, just accept the answer that solved your problem and is all right

2 answers

2


In fact it is quite simple, only use the method format() class Datetime

date_default_timezone_set('America/Sao_Paulo');    
$data_ult_aces = $row_ult_aces['data_acesso'];
$ob_data_atual = new DateTime($data_atual);
$ob_data_ult_aces = new DateTime($data_ult_aces); 

$data_atual_formatada = $ob_data_atual->format('d/m/Y');
$data_ult_aces_formatada = $ob_data_ult_aces->format('d/m/Y');

1

To print in Brazilian format just use the date(); format:

date('d/m/Y');

Where d/m/Y are respectively Day Month and Year

In the Link the information of the possible parameters, which is also usable in the query.

To convert just use :

$dataAmericana = "2010-03-21";
$dataBr = date("d/m/Y", strtotime($dataAmericana));

In case you wanted to convert directly from your Mysql query just use the date_format(), for example:

SELECT DATE_FORMAT("2017-06-15", "%d/%m/%Y");  

In this example I would have the return 15/06/2017, in use of a query it would be sufficient to exchange the static date for the type column date. Example:

SELECT DATE_FORMAT(created_At, "%d/%m/%Y") from table;
  • so I got some ideas on how to change from this library but only returns error in line and does not format the date

  • @Wesleyroberto Which error occurs? and I didn’t understand when speaking library, date(); is native to php.

  • Notice: Undefined variable: data_current in C: xampp htdocs ultimoacesso Adm app adms listar list_usuario.php on line 96

  • I spoke blibioteca for containing many other options, not for being a blibioteca type js, but it did not work return this error always even with the ideas of friends below, ta complicated.

  • @Wesleyroberto guy this error has nothing to do with the function of converting, this variable $data_atual does not exist, your problem is there. You are using a variable that has not been defined.

  • valeu galera resolvi usando $data = new Datetime($data_ult_aces); echo $data->format(’d/m/Y H:i:s'); .

Show 1 more comment

Browser other questions tagged

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