How to arrange the date order?

Asked

Viewed 596 times

2

In the database:

inserir a descrição da imagem aqui

Showing on page (in php):

inserir a descrição da imagem aqui

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

3 answers

3


You can use strtotime, thus:

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

See on IDEONE.

1

You could do it this way 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 also another way could be using array and string manipulation functions:

$parts = array_reverse(explode('-', $data));
echo implode('-', $parts);//01-11-2015

1

You can use this function I made for my website:

<?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 the time as well, by changing the parameter of the "-" for ":".

Browser other questions tagged

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