Solved

How do I insert time into a mysql database

Posted on 2013-06-29
8
539 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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
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 200 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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 200 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

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!

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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