views:

339

answers:

4

MySQL stores the date in my database (by default) as 'YYYY-MM-DD' The field type for my date is 'DATE' (I do not need any time storage).. Is there a simple way to change it by default to DD/MM/YYYY ?

I call up different dates in 2 different tables, and no where in any of my code do I have anything resembling a date variable or anything! Hopefully this is a straight forward change?

+3  A: 

Just use the Mysql built in function DATE_FORMAT()

SELECT DATE_FORMAT(some_date_field, "Y/m/d");
John Conde
This, except the format is wrong in the example. :)
Mikael S
+3  A: 

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 :

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
Pascal MARTIN
Where do I use this date function and how?
Jess
I've edited my answer to add more informations ; is your comment-question still valid ? If yes, which possible solution does *"this date function"* refer to ?
Pascal MARTIN
I'm just a bit confused where I insert it! :S Sorry, I'll have have a play and work it out :)
Jess
If you choose one of the PHP solutions, you can do that somewhere arround your display, as it's a matter of presentation ;;; if you choose the SQL solution, well, obvisouly, you'll do that in your SQL query *(But considering this is presentation, I would prefer having this in the view/template, in PHP)*
Pascal MARTIN
Ok, what would you say is the best solution to use if I am planning on sorting the table rows by date (afterwards), and also running a query that displays items with dates that have passed a deadline, from what the system date is (if that makes sense)...e.g. system date is set as 20th march 2010, a deadline for activity is set as 21st march...i plan on changing the colour of the table row to red (just an example) to shows its passed date!Will using PHP or MySQL to format the date make any difference to my requirment?
Jess
No, formating in PHP or MySQL should not make much of a difference : the `order by` clause in your SQL query will still work on the original field ;;; having the YYYY-MM-DD date available in PHP might be easier to compare it with the deadline, though : with this format, dates can be compares alphabetically, and YYYY-MM-DD is understood by both `strtotime` and `DateTime`, which means it'll be easier to manipulate ;;; so, I would only do the conversion to DD/MM/YYYY at display-time, I'd say.
Pascal MARTIN
OK thanks for your time Pascal, I will try this out. Your help is much appreciated!
Jess
A: 

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.

St. John Johnson
A: 

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

D.Martin