如何从数据库中获取两个日期之间的值?

时间:2014-11-04 11:54:34

标签: php mysql

我正在创建酒店预订申请。如何在两个日期之间获取数据。

这是我的控制器逻辑。

public function GetMarriageDate(){

   $frm = $this->input->post('from');

   $to = $this->input->post('to');

   $date=$this->db->query("SELECT * FROM `tbl_marriagebooking` WHERE (to_date BETWEEN '$frm' AND $to')UNIONSELECT * FROM `tbl_marriagebooking` WHERE (from_date BETWEEN '$frm' AND to')");

   $count=$date->num_rows();return $count;

}

我的数据库table(tbl_marriageBooing)

id   name        to_date        from_date

1    name 1     2014-11-15    2014-11-18

2    name 2     2014-11-20    2014-11-24

3    name 3     2014-11-26     2014-11-30   

我的问题是,如果有人在这种情况下从to=2014-11-20选择日期2014-11-23,则sql会返回或选择id = 2,因为来自2014-11-20的{​​{1}}已被预订

1 个答案:

答案 0 :(得分:0)

尝试

$frm = date('Y-m-d', strtotime($this->input->post('from')));
$to =  date('Y-m-d', strtotime($this->input->post('to')));
$date=$this->db->query("SELECT * FROM tbl_marriagebooking WHERE DATE(to_date) >= '$to' AND DATE(from_date) <= '$frm'");

对于两个日期完全匹配

$date=$this->db->query("SELECT * FROM tbl_marriagebooking WHERE DATE(to_date) = '$to' AND DATE(from_date) = '$frm'");