我需要获取当前时间小于 2 小时和大于当前时间 2 小时的数据。在 DB 表中,我创建了 2 个字段,一个是开始时间,另一个是关闭时间。这两个字段都是 VARCHAR 格式。在我的数据库表下方。
http://i.imgur.com/JJK5c21.png
我已经创建了 mysql 查询,但这不起作用。
select *
from table_name
where TIME_FORMAT('states','h:i %A') > '$curr-2 '
and TIME_FORMAT('close_time','h:i %A') < '$curr+2'
$curr-2 = Current time - 2 Hours (minus)
$curr+2 = Current time + 2 Hours (Plus)
此 SQL 不起作用。
最佳答案
如果字段是datetime
或time
,那肯定会更有意义吗?但是,以下内容可能会满足您的需求...
select * from `table_name`
where
time_format( time( now() ),'%h:%i')
between
time( concat( time_format( `states`,'%h')+2,':',time_format( `states`,'%i') ) )
and
time( concat( time_format( `close_time`,'%h')+2,':',time_format( `close_time`,'%i') ) )
关于php - 通过mysql获取时间间隔的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33184124/