Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL

You can display you date in any format you want in your pages, in mysql i realy don't know, in php you can use this function: date ( string $format [, int $timestamp ] ). So you can do this:

echo date( "d/m/Y", strtotime ( $your_date ) );

You can find full description here: http://php.net/manual/en/function.date.php


Just use the Mysql built in function DATE_FORMAT()

SELECT DATE_FORMAT(some_date_field, "Y/m/d");

After getting it from the DB, use $time = strtotime($value) to convert it to a timestamp in PHP. Then use date("d/m/Y", $time) to get it in that format.


In PHP, you could :

  • Transform the date to a timestamp, using strtotime
  • Format it, using date

A bit like this, I'd say :

$timestamp = strtotime($date_from_db);
echo date('d/m/Y', $timestamp);

But this will only work for dates between 1970 and 2038, as timestamps are stored as 32 bits integers, counting from 1970-01-01.


In MySQL, I suppose the date_format function would do the trick.
For example :

mysql> select date_format(curdate(), '%d/%m/%Y');
+------------------------------------+
| date_format(curdate(), '%d/%m/%Y') |
+------------------------------------+
| 19/03/2010                         |
+------------------------------------+
1 row in set (0.03 sec)


And, for the sake of completness, another solution, in PHP, that doesn't suffer from the limitation of 1970-2038 would be to use the DateTime class, and, especially :

  • DateTime::__construct to parse the date returned by the DB
  • DateTime::format to format the date to whatever format you want.

For example, this portion of code :

$date = new DateTime('2010-03-19');
echo $date->format('d/m/Y');

would get you this output :

19/03/2010

Tags:

Mysql

Sql

Php

Date