Im very confused. For example I’m adding a record which time should be also filled up, then I filled up the the time textbox with format like this 6:30 pm.
So how could I store this data into mysql database? What data type should I use?Because there are time,datetime and timestamp datatypes. What PHP functions should I use.
What exactly I wanted to achieve is to get user inputted time with the format H:MM am/pm (e.g 6:30 pm), store it in a database the proper way, and display or retrieve this data with formatting exactly the way it was entered.
Could anyone please help me understand how to store and retrieve times in mysql using Php. Please give me examples.
I know there are so many resources on the web, but I want direct and clear answers for just a newbie like me in programming.
If you take the time as
6:30 PMas user input then you can usestrtotime()to convert it to a timestamp, which you can then format for storage in the database.Use the MySQL
TIMEdata type which requires the time format in HH:MM:SS (24 hour).Example:
Produces:
This is ready to insert into the database. When you retrieve it from the database, you can have MySQL format it back to the original format using the
DATE_FORMAT()function.Produces: