• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

How do I insert time into a mysql database

I've got a start time I'm receiving from a form that my user will fill out that has:

hour
min
am/pm

How do I construct the sanitization and the insert code?

The mysql database column that it's going into is formatted as "TIME."

I don't want to do a timestamp that includes the date, I just want hours and minutes with the AM / PM thing.

How do I do that?
0
brucegust
Asked:
brucegust
5 Solutions
 
GaryCommented:
The same as you would with any other field if you are MySQL_query
0
 
Dave BaldwinFixer of ProblemsCommented:
From http://dev.mysql.com/doc/refman/5.0/en/time.html ...

MySQL retrieves and displays TIME values in 'HH:MM:SS' format

If you have it in any other format, you need to combine or convert it into that format.

http://www.php.net/manual/en/class.datetime.php
0
 
Ray PaseurCommented:
Please see this article:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

You can use strtotime() to convert a string value into a timestamp, and you can render the HH:MM:SS value using date().  But beware of using the time alone without a date value.  As the article shows, some arithmetic functions will not work the way you might want.

Any special reason why you want to store a time without any date information?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jagadishwor DulalBraces MediaCommented:
Get Current Time using date Function like below will return server current time:
<?php
$time= date('H:i:sa');
echo $time;
?>

Open in new window


you can add  gmt time.
http://php.net/manual/en/function.date-default-timezone-set.php
0
 
GaryCommented:
Convert it to 24 hour format for inserting
$Time_24hour  = date("H:i", strtotime( $hour.":".$min.":".$secs ) );

Convert it to 12 hour format after retrieving
$Time_12hour = date("g:i a", strtotime($db['field']));
0
 
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_brucegust.php

<?php // RAY_temp_brucegust.php
error_reporting(E_ALL);

// MAN PAGES:
// http://dev.mysql.com/doc/refman/5.0/en/time.html
// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-literals.html

// TEST DATA
$hour = '11';
$min  = '38';
$ampm = 'pm';

// MAKE A TIMESTAMP AND RENDER THE ISO-8601 STANDARD TIME
$ts = strtotime("TODAY $hour:$min $ampm");
$iso = date('H:i:00', $ts);

// SHOW THE WORK PRODUCT
echo $iso;

Open in new window

0
 
GaryCommented:
$Time_24hour  = date("H:i", strtotime( $hour.":".$min.":".$secs ) );

Should be

$Time_24hour  = date("H:i", strtotime( $hour.":".$min.":".$am_pm) );
0
 
brucegustAuthor Commented:
Excellent!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now