myqsl - inserting time into database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
alexislalas
Forum Commoner
Posts: 44
Joined: Sun Feb 19, 2006 10:09 pm

myqsl - inserting time into database

Post by alexislalas »

i have a menu/list for the hours and for the minutes. whats the syntax to insert them into the database. i asigned that field the time datatype.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

HH:MM:SS on a 24 hour time cycle... 11:23 PM would be 23:23:00.
alexislalas
Forum Commoner
Posts: 44
Joined: Sun Feb 19, 2006 10:09 pm

Post by alexislalas »

yeah, but how would the sql syntax look like:

INSERT INTO table (variable) values (????)


if my variables have names like: hr, min, sec should it look something like INSERT INTO table (variable) values (hr:min:sec) or is that not capable?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You construct the time string before inserting it, or you can do it during insert.

Code: Select all

INSERT INTO table (timefield) VALUES ('23:23:00');
For MySQL I would use backticks around the fieldnames, especially if there is a chance of a reserved name conflict...

Code: Select all

INSERT INTO `table` (`timefield`) VALUES ('23:23:00');
If your time stamp is comprised of variables, you can assign those values at insert time...

(using PHP)

Code: Select all

<?php
$sql = "INSERT INTO `table` (`timefield`) VALUES ('$hour:$minute:$second')";
?>
alexislalas
Forum Commoner
Posts: 44
Joined: Sun Feb 19, 2006 10:09 pm

Post by alexislalas »

thanks everah!!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No problem. Glad it helped.
Post Reply