【问题标题】:Disabled date if there is a date between start and end php mysql如果开始和结束 php mysql 之间有日期,则禁用日期
【发布时间】:2017-12-12 16:13:32
【问题描述】:

我有一个带有日期值的用户表数据,当我想在有新日期并且新日期在现有日期时发出通知时,我有点困惑,它会收到警告。如何解决这个问题

tb_user
+----+--------+------------+------------+
| id | user   |   Start    |   Finish   |
+----+--------+------------+------------+
| 1  | John   | 2017-12-05 | 2014-12-06 |
| 2  | John   | 2017-12-07 | 2014-12-09 |
| 3  | Smith  | 2017-12-03 | 2014-12-06 |
| 4  | Smith  | 2017-12-07 | 2014-12-10 |
+----+--------+------------+------------+
new value john = start 2017-12-08 and finish 2017-1210

例如,从上表中,我将代表 john 添加一个新日期,从 2017/12/08 开始并在 2017/12/10 结束,因为 2017/12/08 日期已经在 2017/12/ 之间07 - 2017/12 / 09 然后我无法通过显示标题添加数据。我已经查询过了,但有点不确定如何解决?

$maxDate = $this->db->query('select max(start) as maxD, min(finish) as minD  from tb_user where user = "'.$row->user.'" ');
$maxDate = $maxDate->row_array();

if($row->start < $maxDate['maxD'] ){
    echo '<label class="text-danger">booking</label>';
}else{
    echo '<label class="text-success">empty</label>';
}

【问题讨论】:

    标签: php mysql codeigniter codeigniter-3


    【解决方案1】:

    想要的是一个已经属于另一个时间跨度的开始日期。 如果它位于另一个时间跨度中,则时间跨度的开始日期将小于或等于您的开始日期,而结束日期将更大或等于。作为一个条件,它表示为:start &lt;= date AND finish &gt;= date。 符合此条件的时间跨度将与您的新时间跨度重叠。

    在您的尝试中使用MINMAX 的问题是,您可能在时间跨度上存在差距,这不会被视为这种方式。

    您可以将此条件添加到您的 SQL 查询中。这可以通过使用您已经使用的 Codeigniter query 方法来完成,它可以让您处理转义和所有其他事情,或者通过使用 CodeIgniter QueryBuilder 方法来完成,它将为您完成所有事情。

    使用查询方式:

    $maxDate = $this->db->query('SELECT * FROM tb_user WHERE start <= "' . $row->start . '" AND finish >=  "' . $row->start . '"');
    

    使用查询构建器:

    $this->db->select(); // Giving no parameter is equal to SELECT *
    $this->db->from('tb_user');
    $this->db->where('start <=', $row->start); // $this->db->where('fieldname operator', value);
    $this->db->where('finish >=', $row->start); // If you do not add an operator to the first parameter the operator will be = by default
    $maxDate = $this->db->get();
    

    为确保时间跨度不重叠,您还应检查行的结束日期:

    使用查询方式:

    $maxDate = $this->db->query('SELECT * FROM tb_user WHERE ( start <= "' . $row->start . '" AND finish >=  "' . $row->start . '" ) OR ( start <= "' . $row->finish . '"AND finish >=  "' . $row->finish . '" )');
    

    使用查询构建器:

    $this->db->select(*);
    $this->db->from('tb_user');
    $this->db->where('start <=', $row->start);
    $this->db->where('finish >=', $row->start);
    $this->db->or_where('start <=', $row->finish);
    $this->db->where('finish >=', $row->finish);
    $maxDate = $this->db->get();
    

    此条件仅在没有括号的情况下有效,因为 AND 运算符比 OR 运算符强,并且不存在其他 where 子句。如果您必须添加其他 where 子句,您还需要在查询构建器中添加 paranethsis。考虑一下,最后一个 where 方法具有与其他方法不同的结构。因为您需要将右括号添加到查询中,所以您需要将其添加到右侧,因此添加到值侧。查询构建器还转义了第二个参数,因此也会转义右括号。为了防止可能只给where 方法一个参数,包含转义值和右括号。

    $this->db->select(*);
    $this->db->from('tb_user');
    $this->db->where('(start <=', $row->start);
    $this->db->where('finish >= ', $row->start);
    $this->db->or_where('start <=', $row->finish);
    $this->db->where('finish >= "' . $row->finish . '")');
    $maxDate = $this->db->get();
    

    查询将返回所有发生冲突的行,因此只有当此查询的结果为空时,才能添加新日期。这可以使用num_rows 方法进行检查。

    if ($maxDate->num_rows() > 0) {
        echo '<label class="text-danger">booking</label>';
    } else {
        echo '<label class="text-success">empty</label>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多