Solved

How do I insert time into a mysql database

Posted on 2013-06-29
8
498 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 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 109

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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 109

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

776 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