CodeIgniter Query Builder - 获取未来 2 天内的数据

CodeIgniter Query Builder - Get data with date within next 2 days

背景:

用户可以创建预订。他们可以为所述预订设置 'required by' 日期和时间。这作为日期时间格式进入数据库。

我有一个页面需要显示未来两天内即将到来的预订,而不是在

之后

问题:

我无法将数据限制在 2 天内。

目前我可以用它来检索所有即将到来的预订。

当前代码

    $this->db->select('*');
    $this->db->from('nh_bookings');

    $this->db->where('nh_bookings.booking_req_by_date_time > DATE_SUB(now(), INTERVAL 2 DAY)');

    $query = $this->db->get();
    return $query;

如有任何帮助,我们将不胜感激!

您可能需要这样做:

$this->db->where('nh_bookings.booking_req_by_date_time >=', date('Y-m-d H:i:s', strtotime($start_date." 00:00:00")));
$this->db->where('nh_bookings.booking_req_by_date_time <=', date('Y-m-d H:i:s', strtotime($end_date." 23:59:59")));

或者:

$this->db->where("nh_bookings.booking_req_by_date_time BETWEEN ".date('Y-m-d H:i:s', strtotime($start_date." 00:00:00"))." AND ". date('Y-m-d H:i:s', strtotime($end_date." 23:59:59")));
$this->db->where('nh_bookings.booking_req_by_date_time < DATE_ADD(now(), INTERVAL 2 DAY) AND nh_bookings.booking_req_by_date_time > NOW()');

检索从现在到接下来两天的项目。

还要记住 where() 方法有第三个参数 $escape,默认为 TRUE。此参数可能导致 Mysql 函数转义,因此它们无法正常工作