Mankind1023
Mankind1023

Reputation: 7752

Mysql how to set time data type to be only HH:MM in database

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.

Upvotes: 6

Views: 17346

Answers (2)

Rookz
Rookz

Reputation: 146

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));

https://www.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

Upvotes: 6

Sandy
Sandy

Reputation: 862

use TIME_FORMAT(time,format) function of mysql

Upvotes: 0

Related Questions