Store date in mysql table as dd/mm/yyyy instead of yyyy-mm-dd

Experts,

This is the first time i have ever worked with mysql/php, i have managed to store records in the DB including a date field. However, the only way the DB will accept the date is if it is of the format yyyy-mm-dd. I would like to store and retrieve in the format dd/mm/yyyy.

Please can someone advise how i change this on mySql DB. I currently access the DB via 'phpMyAdmin', yet cant seem to find how i would do this.

Regards
Easynow
LVL 12
Dean OBrienAsked:
Who is Participating?
 
Loganathan NatarajanConnect With a Mentor LAMP DeveloperCommented:
best thing, i would suggest to store as it "yyyy-mm-dd" then you split like ,
<?php
 
$date = "1973-05-20";
list($month, $day, $year) = split('[/.-]', $date);
 
echo "Month: $month; Day: $day; Year: $year<br />\n";
 
?> 

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperCommented:
you can use split() function to split and concatenate .., then store and retrieve it.,
0
 
gamebitsCommented:
This the way date field works in mysql you could use another type instead of date (VARCHAR or CHAR for example).

Another way is to use unix timestamp and use one of the php function to manipulate the data.

http://ca3.php.net/time
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Loganathan NatarajanLAMP DeveloperCommented:
you cannot store in mysql db like dd/mm/yyyy .. but you can manipulate it whereever you want using split() in what ever the format
0
 
Loganathan NatarajanLAMP DeveloperCommented:


list($year, $month, $day) = split('[/.-]', $date);

Open in new window

0
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
I confirm: the MySQL date is in format YYYY-MM-DD and you should NOT try to change that.
if you used a VARCHAR field instead, with format MM/DD/YYYY or DD/MM/YYYY, you WILL come back later crying when you try to sort by date and it won't ...

so, the DATE is just fine, and to reformat it for display you shall use the DATE_FORMAT() function in MySQL:
dev.mysql.com/doc/mysql/en/date-and-time-functions.html
0
 
Dean OBrienAuthor Commented:
Ok thanks for the rapid response. The solution you proposed seems ideal, i will try and implement it cheers.

Seems annoying that they cant have date types though.... seems i will have to mask the date when selecting it, change it to a different form to save it, then change again to display it upon retreival....


Thanks for the posts.
Easynow
0
 
Sander StadConnect With a Mentor Systeemontwikkelaar, Database AdministratorCommented:
In the beginning I was confused how to get the dates right. I finally found a solution and that was the time() function to create a timestamp.

The easy thing is that you can create any date from it using the date() function. This website could probably help you: http://www.plus2net.com/php_tutorial/php_date_time.php
0
 
Dean OBrienAuthor Commented:
Sorry for delayed closure, really appreciated the help. I have now completed what i was after.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.