Convert date format to php

Asked

Viewed 915 times

4

Personal I am working with Wordpress php and get from the database a date in the following format: "22 October, 2018", more need to convert it to "2018-10-22", how do I get that?

tried:

date('Y-m-d', strtotime(get_post_meta( $id_pedido, "Data de entrega", true )));

The value returned is "1970-01-01" and not the date reported. Thank you!

  • 2

    Need to adapt a little more in general lines is this => How to make a full date a common date?

  • 1

    Dude, Outubro is in Portuguese even?

  • 1

    Yeah, bro. I tried those answers and none of them worked.

  • 1

    @wDrik doesn’t have that date the bank doesn’t? You really need to take the one that comes from WP? Or WP saves it so in the bank (if it is, it is WP’s mess)

  • So bro is how wordpress is returning. = / do a query for this I think will give more trampo! kkkkk

  • get_post_meta( $id_request, "Delivery Date", true ) is returning the date correctly?

Show 1 more comment

4 answers

3

You can use the function substr() of PHP. Ex:

$data = "22 Outubro, 2018";

$ano = substr($data,-4); //4 dos últimos 4 caracteres e o "-" para começar a contar do final.

$dia = substr($data,0,2; //0 do ponto inicial e 2 para dois caracteres a partir de 0.

$mes_array = explode(" ", $data);//Separa nos espaços a string "data" e transforma em um array.
$mes = $mes_array[1];//Seleciona o a segunda posição do array.

2

You can assemble something simpler than suggested in the link. The first step is to define two arrays one for search and one for substitution (from Portuguese to English) because the names of the months or days of the week cannot be converted into a valid date if specified in Portuguese. Note that the first element of $en is a glass that will be replaced by nothing.

After cleaning (replaced with str_ireplace()) the string just call the method createFromFormat() of DateTIme defined the input format that in the case is the day (d) following the month in full (F) and last the four-digit year (Y).

format() sets the format you want Y-m-d.

$pt = [',', 'janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho', 'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro']; 
$en = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

$data = DateTime::createFromFormat('d F Y', str_ireplace($pt, $en,'22 maio, 2018'));
echo $data->format('Y-m-d');

Example - ideone

1


Personal thanks to all I managed to settle as follows:

$data_de_entrega = get_post_meta( $id_pedido, "Data de entrega", true );
$pt = [',', 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro']; 
$en = ['','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$data_new = str_replace($pt, $en, $data_de_entrega);

echo date('Y-m-d', strtotime($data_new));

-1

Try to do that:

$tempo = strtotime("22 October 2018");
echo date("Y-m-d", $tempo);

Remembering that the function expects to be informed a string containing a date format in English US, in this case, "22 October 2018".

Note: This is also for all other months, that is, you should add the month written in English.

For more information: http://php.net/manual/en/function.strtotime.php

  • 2

    -1 The question is as "October" and not "October". Your answer does not apply.

  • 1

    @Wallacemaxters respect his opinion, however, who accepted and evaluated this answer as the best answer was the author of the question, being useful to him (good that helped him). In my view it is he who has the duty to tell me whether my answer applies or not, however, I appreciate your feedback!

  • Wrong. You’re not just responding to the AP, you’re responding to the community. The people who enter here should benefit from the answers placed here. This should be something that both the questioner and the respondent should be aware of.

Browser other questions tagged

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