Convert Mysql date (YYYY/mm/dd h:m:s) to dd/mm/yyyy

Asked

Viewed 4,234 times

4

Check out my database:

inserir a descrição da imagem aqui

I’m using the following code:

<?php 
$data = $noticia->data_cadastro;
setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese");
date_default_timezone_set("America/Sao_Paulo");
echo strftime("%A, %d de %B de %Y", strtotime($data));
?>

Content of the variable: $data: var_dump: "string '09/02/2015 15:55:30' (length=19)"

Returns in format: "Wednesday, 02 September 2015".

I would like you to return "Monday, February 9, 2015"; and then customize to "09 February 2015";as I do?

Differs from posting:"Format date in PHP" for where is echo strftime( '%A, %d de %B de %Y', strtotime('today')); in my case has a variable $date, simple, but it’s making a difference in my case.

  • 3

    You mean 09 de Fevereiro?

  • @Oeslei, 9 February 2015, but should be displayed as "02 February 2015", layout...

  • Will never display "02 February 2015" if its date is "2015-02-09 (2015 February 09)"

  • @Luishenrique, similar, changing "strtotime('Today'));" to "strtotime('$data'))" and did not work...

  • The field guy is marked as what?

  • Confusing question! :\

  • @Wallacemaxters, see if you’ve improved.

Show 2 more comments

4 answers

4

If you want to return only 09 Fevereiro 2015, then do it this way:

setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese");

echo strftime("%d %B %Y", strtotime($data));

In his example, there seems to be an error of interpretation between mm and dd, and vice versa.

In this case, this solution below would work, but I still recommend that you identify if there is any date formatting error caused by regionalization of the same.

$data = DateTime::createFromFormat('m/d/yy', $noticia->data_cadastro)->getTimestamp();

echo strftime("%d %B %Y", $data);

See that we create the date from the format m/d/yy, and not as d/m/yy, because of the formatting of your date.

Updating

However, if you want to do exactly as it is in the title of the question, that is "Convert Mysql date (YYYY/mm/dd h:m:s) to dd/mm/yyyy", then you can do so.

echo DateTime::createFromFormat('yy/m/d H:i:s', $data)->format('d/m/Y');
  • Still returns "02 September 2015"

  • @Adrianoaquino must have another problem then, see working on https://ideone.com/5C4Ifs

  • @Adrianoaquino Maybe you saved the wrong date in the bank. Check directly in the bank the saved date.

  • 1

    In this case it is format problem. Maybe a DateTime::createFromFormat('m/d/yy', $data)->getTimestamp(). 'Cause I see he plays the dd as mm and the mm as dd.

  • @Oeslei, look at the comic book print...

  • @Wallacemaxters I think he wants to return: Month in Decimal Number + Month Name + Year. That’s what I got.

  • @Wallacemaxters, that’s correct, but look at the contents of the variable: "string '09/02/2015 15:55:30' (length=19)".

  • What do you mean, @Adrianoaquino? You didn’t post anything in your question with this format? It induced my answer the way it was put. If there is a var_dump with this result, please inform in your question. So maybe I can complete my answer in the most appropriate way to your problem.

  • @Wallacemaxters, had not thought to use a var_dump earlier, I’m starting in PHP, had focused only on BD... sorry.

Show 4 more comments

4


This should work:

setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese");
date_default_timezone_set('America/Sao_Paulo');

$olddata = '09/02/2015 15:55:30';
$data = str_replace('/', '-', $olddata);

// Formato para por no Layout (pedido antes da edição da pergunta)
echo strftime("%m %B de %Y", strtotime($data)); // 02 fevereiro de 2015

// Como pedido após a edição da pergunta, personalizar a data para 09 de fevereiro de 2015
echo strftime("%d de %B de %Y", strtotime($data)) // 09 de fevereiro de 2015 

DEMO

  • In the example above it works, qdo adds the variable does not work. See the contents of the $data "string '09/02/2015 15:55:30' (length=19)".

  • Almost, only left to return in pt-BR, see how it is: "02 Feb 2015".

  • When you enter the variable "$data = $noticia->data_cadastro;", the format is "02 setembro 2015", which can be?

  • This way it works: "$data = "2015-02-09 15:55:30";". But the variable $data receives the result of the method: "$data = $noticia->data_cadastro;" in the following format (var_dump): "string '09/02/2015 15:55:30' (length=19)" and then stays in the unwanted format "02 September 2015".

  • Returns "02 February 2015", help me convert to pt_BR, I’m not getting...

  • @Adrianoaquino Run this code to see if it changes something. = D

  • @Adrianoaquino If it still doesn’t work, call me at chat. :)

  • OK, I paused this part of the project, rsrsrs

  • @Adrianoaquino quit? haha, did you even test that last code I posted? I even installed PHP just to see if it happened to me, but it didn’t happen. = D

  • And... tried to see, and did not turn, must be some problem with my machine, who knows a formatting in the S.O. solves... But vlw by the help! =)

  • @Adrianoaquino Vish! If possible mark any of the answers as accepted. :-)

Show 7 more comments

4

You can do this directly in the mysql query

Using the DATE_FORMAT()

Example

SELECT *, DATE_FORMAT(data_cadastro,'%d/%m/%Y') AS data_formatada FROM sua_tabela 

2

I don’t know if it works on MYSQL, but in the PostgreSQL would be equivalent to

to_char(data_cadastro, 'DD/MM/YYYY')

Browser other questions tagged

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