【问题标题】:Check whether time ranges exists between two given start and end times检查两个给定的开始和结束时间之间是否存在时间范围
【发布时间】:2019-07-08 13:00:11
【问题描述】:

我在数据库中有两个字段分别命名为start_timeend_time 我想知道用户输入的value 是否存在于这些时间范围之间,如果存在则只有我的函数返回true 否则false。为此,我编写了此查询,但它无法正常工作。开始时间包含我已转换为的 AM PM 格式的时间,例如:7:30 AM9:30 AM 分别表示开始和结束时间。请帮助解决我的问题。

$this->db->group_start()->where('TIME(start_time) <=',
date('H:i:s',strtotime($start_time)))->where('TIME(end_time) >=',
date('H:i:s',strtotime($end_time)))->group_end()->or_group_start()->where('TIME(start_time) <=',
date('H:i:s',strtotime($end_time)))->where('TIME(end_time) >=',
date('H:i:s',strtotime($start_time)))->group_end();

【问题讨论】:

    标签: php mysql codeigniter codeigniter-3


    【解决方案1】:

    您可以简单地比较 MySQL Query 中的时间,只要您对所有条目使用相同的格式

    $time = "7:31 AM";
    $this->db->where('start_time <=', $time);
    $this->db->where('end_time >=', $time);
    

    原始查询:

    SELECT * FROM `table` WHERE `start_time`<='7:35 AM' AND `end_time`>='7:35 AM'
    

    【讨论】:

    • 谢谢,但我不必将它与当前时间进行比较。将有两个输入字段开始时间和结束时间,它们将接受用户输入,这些时间范围将与数据库列进行比较,例如。开始和结束。在我上面的查询中,我采用了两个变量,即 $start_time 和 $end_time
    • 您能否举例说明您的程序何时应使用示例数据返回真或假?我想你想检查用户给定的时间范围是否在数据库的时间范围之间,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多