Format date and time with PHP

Asked

Viewed 12,319 times

7

I have this page that displays the emails received from the logged in user, along with the respective dates. I’m trying to format the date display by PHP but it’s not working as I expected. I need the PT-BR date to appear in the first line and the hour and minute in the second line, but it is appearing according to the image. Can you help me ? Follows my code:

<? foreach ($resultado as &$rowmensagens) {
$datamensagem = $rowmensagens['dataenvio'];
$datamensagem = date("d/m/Y H:i:s", strtotime($datamensagem));
$horamensagem = date("H:i:s", strtotime($datamensagem));

$arr_msg = explode("/", $datamensagem);
$diamsg = $arr_msg[0];
$mesmsg = $arr_msg[1];
$anomsg = $arr_msg[2];

$arr_hora = explode(":", $horamensagem);
$hora_msg = $arr_hora[0];
$minuto_msg = $arr_hora[1]; ?>

<strong><?=$diamsg?>/<?=$mesmsg?>/<?=$anomsg?></strong>
<strong><?=$hora_msg?>:<?=$minuto_msg?></strong>

NOTE: This formatting already worked on another occasion and I just copied the structure to this page. Thank you !

  • Have you tried: date("d/m/Y", strtotime($datamensagem)); instead of date("d/m/Y H:i:s", strtotime($datamensagem)); ?

  • @cat Yes... it removes the time from the first line but in the second line continue to appear all records as 21:00

  • No need to ask the solution question and mark SOLVED the answer with the green check already indicates the solution.

  • Read about accepted answer.

2 answers

11


I would suggest you use this method will be much simpler you can do it this way

(new DateTime($datamensagem))->format('d/m/Y H:i:s');
  • 2

    It worked, thank you !

  • 2

    This reference also helped me a lot, thanks.

9

Well, you’re complicating your code to tell the truth... the function date() already formats the pro date you need, even you are already formatting it there, just need to adjust the same format:

$envio = strtotime($rowmensagens['dataenvio']);
$datamensagem = date("d/m/Y", $envio);
$horamensagem = date("H:i", $envio);

and then just print. Note that you don’t need to separate the string or anything, just use.

About the error that appeared only the time 21:00, occurs because the function strtotime($datamensagem) returns false, which is the value when the function fails. Therefore it would probably be enough to modify the code to convert the string coming from the database, in the format that the function understands, and to save the value only once, passing this value to the function date whenever you need.

Learn more at documentation of the date function and of strtotime function.

  • I tried to do as your example but the time in the second row keeps appearing as 21:00 =(

  • see the edition, if it clarifies something

  • The column has the time value yes... I could not understand why the formatting did not show the time correctly, but it worked already here. Thanks for your help.

  • I’m glad you made it, but just for the guys who get here, I edited the answer with what I found out around here.

Browser other questions tagged

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