How to change date format in PHP [duplicate]

0

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

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'] : ''; ?>

Which returns me like this, example:

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

How do I make the date in the format day / month / year?

    
asked by anonymous 11.05.2018 / 00:45

1 answer

1

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];
    
11.05.2018 / 01:53