views:

29

answers:

2

Hi, how can I set my mysql database field "time" data type to only be HH:MM in the database, in my script the user only enters HH:MM and the DB automatically adds the last :SS digits, the problem is when I pull that value to edit, it adds the last digits also, which is kind of annoying, I can get rid of it with PHP and truncating off the end, but I was hoping for a way to set it in the DB to remove those last 2 SS digits for good.

A: 

use TIME_FORMAT(time,format) function of mysql

Sandy
A: 

I don't believe you can configure your database to store the time without the SS. MySQL's TIME DataType reference: http://dev.mysql.com/doc/refman/5.1/en/time.html

You'll have to set the formatting to HH:MM either in the mysql query or in php after you pull the data.

In PHP:

$date = date('H:i', strtotime($db_date_value));

http://us3.php.net/manual/en/function.date.php

In MySQL:

DATE_FORMAT(date_created, "%H:%i") as date_created

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

Rookz
Most people use their application to define display controls, but if you really want it in the database, you could create a view to always show the data like this, or create an extra column which only stores the time (CHAR) like this. Maybe combine with stored procedures to keep the extra column up to date with the real one.
Konerak