需要建议,在哪种方式,我需要挖掘过滤问题。
我有运动事件的记录,每个运动事件都有事件开始时间和事件结束时间的列:
事件#1
开始时间: 09:00 (time_from)
结束时间: 12:00 (time_to)
数据库中的时间格式为HH:MM.。
然后我有过滤器页面,在那里用户可以设置事件时间的时间范围,以显示09:45 - 11:45进行的所有事件)
我试图通过将过滤输入和数据库值转换为秒来过滤用户指定范围内的事件,但它不能完全工作:
$i = 0;
foreach($sport_events as $event)
{
// check if the event is in user specified range
// $filter_time is already converted to strtotime previously
if( strtotime($event['time_from']) >= $filter_time_from_seconds && strtotime($event['time_to']) <= $filter_time_to_seconds )
{
// do nothing
} else {
// unset sport event
unset($event[$i]);
}
$i++;
}如果用户输入09:00和12:00,则可能会输出结果,而不是在范围内的10:00和11:00。
有什么想法吗?
发布于 2018-07-06 21:16:35
如果您将'from_date‘和'to_date’以时间格式存储到数据库中,那么您可以直接将用户输入的过滤器数据传递到MySQL查询中,如下所示
SELECT * FROM TABLE-NAME WHERE `from_date` >= '10:00' AND `to_date` <= '12:00';你不需要使用循环来检查PHP条件下的结果集。
https://stackoverflow.com/questions/51210816
复制相似问题