Mysql如何在数据库中将时间数据类型设置为只有HH:MM

时间:2021-12-29 16:06:08

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.

如何将我的mysql数据库字段“time”数据类型设置为数据库中只有HH:MM,在我的脚本中用户只输入HH:MM并且DB自动添加最后一个:SS数字,问题是当我拉那个值要编辑,它也添加了最后的数字,这有点烦人,我可以用PHP摆脱它并截断结束,但我希望有一种方法在DB中设置它以删除那些最后2 SS数字好。

2 个解决方案

#1


4  

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

我不相信你可以配置你的数据库来存储没有SS的时间。 MySQL的TIME DataType参考: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.

在拉取数据后,您必须在mysql查询或php中将格式设置为HH:MM。

In PHP:

在PHP中:

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

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

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

In MySQL:

在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

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

#2


0  

use TIME_FORMAT(time,format) function of mysql

使用mysql的TIME_FORMAT(时间,格式)功能

#1


4  

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

我不相信你可以配置你的数据库来存储没有SS的时间。 MySQL的TIME DataType参考: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.

在拉取数据后,您必须在mysql查询或php中将格式设置为HH:MM。

In PHP:

在PHP中:

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

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

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

In MySQL:

在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

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

#2


0  

use TIME_FORMAT(time,format) function of mysql

使用mysql的TIME_FORMAT(时间,格式)功能