【问题标题】:cannot get record which exist between a given time in codeigniter无法获取在codeigniter中给定时间之间存在的记录
【发布时间】:2020-10-07 00:14:24
【问题描述】:

我想获取给定开始时间和结束时间之间存在的记录,开始时间和结束时间将由用户给出,我正在使用以下查询:

$this->db->where('TIME(start) BETWEEN "' . date('H:i:s', strtotime($start_time)) . '" and "' . date('H:i:s', strtotime($end_time)) . '"');
                    $this->db->where('TIME(end) BETWEEN "' . date('H:i:s', strtotime($start_time)) . '" and "' . date('H:i:s', strtotime($end_time)) . '"');
$results = $this->db->get('class')->result();

开始和结束时间列是日期时间字段:

例如。在数据库中 开始时间:2020-06-08 06:15:00 结束时间:2020-06-08 09:15:00

【问题讨论】:

  • 不应该是date('Y-m-d H:i:s', strtotime($start_time))吗?
  • 不,我只想比较时间,我认为问题不在查询中:我可以写这样的东西吗:date('H:i:s'), strtotime($start_time) between TIME ('start') AND TIME('end') AND date('H:i:s'), strtotime($end_time) between TIME('start') AND TIME('end')
  • 因为我只想检查我的开始时间和结束时间是否在数据库开始时间和结束时间之间
  • 尝试使用$this->db->get_compiled_select(); 来查看你编译的查询

标签: php codeigniter codeigniter-2


【解决方案1】:

@user3653474 您可以使用TIMEDIFF 检查提供的时间是否在两列的值之间。看看下面的查询是否符合您的要求。

$start = date('H:i:s', strtotime($start_time)); // get time from date in desired format
$end   = date('H:i:s', strtotime($end_time));

// where $start is between column {start} and {end} AND $end is between column {start} and {end}
$sql   = "SELECT * FROM class 
          WHERE (TIMEDIFF('$start', TIME(start)) >=0 AND TIMEDIFF('$start', TIME(end)) <= 0) 
          AND (TIMEDIFF('$end', TIME(start)) >=0 AND TIMEDIFF('$end', TIME(end)) <= 0)"; 

$results = $this->db->query($sql)->result();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 2021-06-13
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多