?
Solved

How do I insert time into a mysql database

Posted on 2013-06-29
8
Medium Priority
?
566 Views
Last Modified: 2013-07-05
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
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39287596
The same as you would with any other field if you are MySQL_query
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 total points
ID: 39287620
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 39288067
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!

 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39288085
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 800 total points
ID: 39288116
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 39288215
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
 
LVL 58

Accepted Solution

by:
Gary earned 800 total points
ID: 39288339
$Time_24hour  = date("H:i", strtotime( $hour.":".$min.":".$secs ) );

Should be

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

Author Comment

by:brucegust
ID: 39302445
Excellent!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question