MSMC MSMC - 7 months ago 36
SQL Question

Storing html input of type time into MySQL database

hope you fine and well,

i have an html form that contains input of type time, what is the better datatype that i can use to store the input value in MySQL ?!

below is the input:

<td align="center">Time<input type="time" name="start">


i used varchar type, but i think it will cause problems in the future if i need to compare stored time with current time for example.

i didn't used datetime because i just have time only and no need for date.

so, any suggestions?

LMS LMS
Answer

I think the best way to achieve this, still by storing datetime when you create your database (maybe you will need date later for any kind of test ) but when you want to retrive it just use the right format like this :

SELECT DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY

Edit : you can use time format as well :

SELECT TIME_FORMAT(colName, 'HH:MM:SS')

For more information about how to format you can checkout datetime and time

Hope this helps!