3
I’m trying to invert a date variable that displays the results like this:
2016/05/20
But I want a php function that displays like this:
20/05/2016
How to do this?
3
I’m trying to invert a date variable that displays the results like this:
2016/05/20
But I want a php function that displays like this:
20/05/2016
How to do this?
2
You can do that with the data()
and the stringtotime()
thus:
echo date("d/m/Y", strtotime("2016/05/20"));
Another option is to treat as a normal string, split with explode()
, reverse the order with array_reverse()
and merge into a string again with implode()
. Then it would be something like this:
echo implode("/", array_reverse(explode("/", "2016/05/20")));
Example: https://ideone.com/PRRREDi
Thanks friend, it worked properly. Very grateful!
1
I already answered something similar a few days ago, in case I wanted to see the answer is on that topic ... and the concept is the same you should "break" your string in the "/" bars with the function "explode" and then put them back together with the function "implode", i used the function "array_reverse" to do the inversion, but as it is a date you could do it manually.
Example used inversion by PHP
<?php
// Sua data
$data = "2016/05/20";
// Quebra a data nos "/" e transforma cada pedaço numa matriz
$divisor = explode("/", $data);
// Inverte os pedaços da data (via PHP)
$reverso = array_reverse($divisor);
// Junta novamente a matriz em texto
$final = implode("/", $reverso);
// Imprime os resultados na tela
echo $final;
?>
Example inverting "manually"
<?php
// Sua data
$data = "2016/05/20";
// Quebra a data nos "/" e transforma cada pedaço numa variavel
list($ano, $mes, $dia) = explode("/", $data);
// Monta a data de volta no formato certo
$data_nova = ($dia . "/" . $mes . "/" . $ano);
// Imprime os resultados na tela
echo $data_nova;
?>
Thank you friend, it worked properly. Thank you all very much.
0
If you are programming in a procedural way an option is to use the function date_format
:
$date = date_create("2016/05/08");
echo date_format($date,"d/m/Y");
If you are using the Object Orientation approach you can use format
:
$data_string = '2016/05/08';
$data = new DateTime($data_string);
echo $data->format('d/m/Y');
But it is a question with n possible solutions.
References:
Thanks friend, it worked properly. Very grateful!
Browser other questions tagged php string variables
You are not signed in. Login or sign up in order to post.
Since you already have answers, I think you can point to another one. In the other there are even more things than you need, but practically what could come out of answer here is already contained in the one there.
– Bacco