Solved

How do I insert time into a mysql database

Posted on 2013-06-29
8
484 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
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 82

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 108

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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 108

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
delete database record with modal 21 34
Phone Dialer 5 37
using hash in login 34 20
File not deleting using unlink 6 13
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now