Solved

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

Posted on 2008-06-23
9
1,071 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:Dean OBrien
9 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21844658
you can use split() function to split and concatenate .., then store and retrieve it.,
0
 
LVL 28

Expert Comment

by:gamebits
ID: 21844663
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
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21844664
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
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 75 total points
ID: 21844668
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21844670



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

Open in new window

0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 25 total points
ID: 21844678
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
 
LVL 12

Author Comment

by:Dean OBrien
ID: 21844697
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
 
LVL 9

Assisted Solution

by:Sander Stad
Sander Stad earned 25 total points
ID: 21845538
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
 
LVL 12

Author Closing Comment

by:Dean OBrien
ID: 31469668
Sorry for delayed closure, really appreciated the help. I have now completed what i was after.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
<? versus <?php 5 37
Download a website to hdd 2 51
RELATED: How do I fix this error:  Warning: strpos(): Empty needle on line 122 11 32
html input type 3 23
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now