How to change date format in PHP

Asked

Viewed 179 times

0

Hello, I am not an expert in PHP, and I have a question. I have this snippet of code:

Quartos disponíveis para
<i class="awebookingf awebookingf-calendar"></i>
<?php echo isset( $_GET['start-date']) ? $_GET['start-date'] : ''; ?> à 
<i class="awebookingf awebookingf-calendar"></i>
<?php echo isset( $_GET['end-date'] ) ? $_GET['end-date'] : ''; ?>

That returns me like this, example:

" QUARTOS DISPONÍVEIS PARA  2018-07-20 À  2018-07-24

How do for thin date in day/month/year format ?

  • 1

    Related: https://answall.com/q/224948/99718

  • Welcome. Things you should be aware of. How to mark a response as it accepts https://i.stack.Imgur.com/evLUR.png Why accept a reply https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

1 answer

0

Ideone example

//verificação se foram iniciadas
if ((isset($_GET['start-date']) && isset($_GET['end-date'])){

  // As datas
  $dataInicial = $_GET['start-date'];
  $dataFinal = $_GET['end-date'];

  // Quebra as strings nos "-" e transforma cada pedaço numa matriz
  $divisorIni = explode("-", $dataInicial);
  $divisorFin = explode("-", $dataFinal);

  // Inverte os pedaços
  $reversoIni = array_reverse($divisorIni);
  $reversoFin = array_reverse($divisorFin);

  // Junta novamente a matriz em texto separado por tracinho (-)
  $dataIni = implode("-", $reversoIni);
  $dataFin = implode("-", $reversoFin);

      echo 'Quartos disponíveis para';
      echo '<i class="awebookingf awebookingf-calendar"></i>';
      echo $dataIni;
      echo ' à <i class="awebookingf awebookingf-calendar"></i>';
      echo $dataFin;

}

Another way:

$dataInicial = $_GET['start-date'];
$divisorIni = explode("-", $dataInicial);
$dataIni = $divisorIni[2]."-".$divisorIni[1]."-".$divisorIni[0];

Browser other questions tagged

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