How to sort the date?

2

In the database:

Showingpage(inphp):

I would like it to appear as follows: 03-11-2015

    
asked by anonymous 19.11.2015 / 22:06

3 answers

3

You can use strtotime , like this:

$data = "2010-01-02";
echo date("d-m-Y",strtotime($data));
// Saída: 02-01-2010

See the IDEONE .

    
19.11.2015 / 22:12
1

You could do as follows using the DateTime class:

<?php

$data = '2015-11-01';

$date = new DateTime($data);
echo $date->format('d-m-Y');//01-11-2015

Or also using regular expression:

$pattern = '/^([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})$/';
$replacement = '$3-$2-$1';
echo preg_replace($pattern, $replacement, $data);//01-11-2015

Or another way could be to use array and string manipulation functions:

$parts = array_reverse(explode('-', $data));
echo implode('-', $parts);//01-11-2015
    
19.11.2015 / 23:19
1

You can use this function that I made to my site:

<?php
   function data($data){
      $f = explode("-", $data); //Gera um array com 0 = ano, 1 = mês, 2 = dia
      $g = $f[2]."/".$f[1]."/".$f[0]; //Isso é uma string. Formate-a como quiser
      return $g;
   }
?>

You can use it for time too by changing the explode parameter from "-" to ":" .

    
20.11.2015 / 02:30