I have date in dd/mm/yyyy
format. How can I store it in a database, if I want to do some operations on it afterwards?
For example, I must find out the rows, where date > something
. What type I must set for the date
field?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
To store dates or times in MySQL use date
, datetime
or timestamp
. I’d recommend the first two for most purposes.
To tell MySQL how to parse your date format use the STR_TO_DATE function. Here’s an example:
CREATE TABLE table1 (`Date` Date); INSERT INTO table1 (`Date`) VALUES (STR_TO_DATE('01/05/2010', '%m/%d/%Y')); SELECT * FROM table1; Date 2010-01-05
To format the results back into the original form look at the DATE_FORMAT function. Note that you only need to format it if you want to display it as a string using something other than the default format.
Method 2
Use date
if you only care about the date and not about the exact time.
Method 3
or just date
if you don’t need time information
Method 4
use datetime or time stamp and using format functionL
http://davidwalsh.name/format-date-mysql-date_format
http://www.electrictoolbox.com/article/mysql/format-date-time-mysql/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0