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
Just use the Mysql built in function DATE_FORMAT()
SELECT DATE_FORMAT(some_date_field, "Y/m/d");
i'm using this script and put it on upper line.
$(function(){
var pickerOpts = {
dateFormat: "yy-mm-dd"
};
$("#datepicker").datepicker(pickerOpts);
});
and this one in your form.
<input id=**"datepicker" type="date"** name="Start" size="9" value="<?php
echo $Start;
?>" />
It will appear as d/m/y on your page but in your database y/m/d.
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
If you used some Framework like Zend, it would be very easy because you have plenty of classes to handle databases and date formatting. Then you would always see the output as the Brazilian date format. Just like Delphi TDateField uses Windows date format in the current computer, Zend will look up in your own configuration.
In PHP, you could :
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
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.