本文介绍了MySql数据库从日期时间,时间戳记,时间,PHP,SQL中排除A.M,P.M的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有三种数据类型。日期时间,时间戳和时间。我使用日期函数获取时间,并尝试将其插入数据库的所有三列下,但是所有三列都拒绝了日期函数的A.M,P.M部分。我不明白为什么。我还需要插入date函数的A.M,PM.M部分,以便可以按时间更有效地对数据库中的数据进行排序。是否有另一栏可以存储日期的上午,下午和下午部分,还是有解决方法?谢谢。

I have three data types in my database. Datetime, timestamp, and time. I get the time using the date function and tried to insert it into the database under all three columns, but all three columns rejected the A.M, P.M part of the date function. I don't understand why. I need the A.M, P.M part of the date function to be also inserted, so I can sort the data in my database more efficiently by time. Is there another column that can store the A.M, P.M part of the date, or is there a workaround this? Thanks.

$time = date('h:i:s A');
//insert $time into all three columns into the database


推荐答案

解决方法是将 hours 值用作24小时制。要表示 '3:45:56 PM ',请将字符串值 '15插入数据库列:45:56'

The workaround is to use the hours value as 24 hour clock. To represent '3:45:56 PM', insert to the database column a string value '15:45:56'.

否则,您可以使用 STR_TO_DATE 函数将字符串转换为有效的 TIME 值。

Otherwise, you can use the STR_TO_DATE function to convert the string into a valid TIME value.

 INSERT INTO mytable (mycol) VALUES (STR_TO_DATE('3:45:56 PM','%h:%i:%s %p'))






要从以下位置获取 TIME 值:数据库列,格式为12小时制,带有AM / PM。


To retrieve a TIME value from a database column, formatted as 12 hour clock with AM/PM.

SELECT DATE_FORMAT(mytimecol,'%h:%i:%s %p') AS mytimestr FROM ...

参考文献:

这篇关于MySql数据库从日期时间,时间戳记,时间,PHP,SQL中排除A.M,P.M的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 21:29