Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with Time in Php/Mysql

Tags:

php

mysql

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.

like image 225
Towki Avatar asked Dec 05 '12 08:12

Towki


People also ask

Is there a time data type for MySQL?

The date and time data types for representing temporal values are DATE , TIME , DATETIME , TIMESTAMP , and YEAR . Each temporal type has a range of valid values, as well as a “zero” value that may be used when you specify an invalid value that MySQL cannot represent.

How is time stored in MySQL?

MySQL uses the 'HH:MM:SS' format for querying and displaying a time value that represents a time of day, which is within 24 hours. To represent a time interval between two events, MySQL uses the 'HHH:MM:SS' format, which is larger than 24 hours.

How can I insert current time in PHP?

Create a Date With mktime() The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP mktime() function returns the Unix timestamp for a date.

How can store current time in PHP variable?

$current = time(); // save this to column CURRENT_TIME with column type VARCHAR //retrieve it like this $retrieved = mysql_query(....) //assume query for getting the stored time value $time = strtotime($retrieved);


2 Answers

If you take the time as 6:30 PM as user input then you can use strtotime() to convert it to a timestamp, which you can then format for storage in the database.

Use the MySQL TIME data type which requires the time format in HH:MM:SS (24 hour).

Example:

$dbFormat = date('H:i:s', strtotime('6:30 PM'));

Produces:

18:30:00

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.

SELECT DATE_FORMAT(mytime, '%l:%i %p') FROM mytable

Produces:

6:30 PM

like image 118
MrCode Avatar answered Sep 23 '22 21:09

MrCode


You could use timestamp to store your time in the MySQL database.

Time to timestamp:

<?php
    $time = strtotime($yourTime); //yourTime is the time from your input box.
?>

Timestamp to time:

<?php
    $time = date('g:i a', $yourTime); //yourTime is the time from the database.
?>

For more information see: http://nl1.php.net/manual/en/function.date.php and http://php.net/manual/en/function.strtotime.php

Edit: You can save a timestamp value as INT(11) in your MySQL database.

like image 35
Dillen Meijboom Avatar answered Sep 24 '22 21:09

Dillen Meijboom