简体   繁体   English

选择日期在范围内的位置

[英]Select where date falls within range

Codeigniter/PHP: Codeigniter / PHP:

This is my current db query: 这是我当前的数据库查询:

$pass_check = $this->CI->db->select('code')->from('coupons')->where(array('title'=>'Booyah'))->get();
$pass_check = $pass_check->result_array();

There are two other fields: 'exp_date', and 'start_date'. 还有其他两个字段:“ exp_date”和“ start_date”。 These represent when the coupon should be available and when it should not. 这些代表优惠券何时可用,何时不可用。

How can I only select codes/coupons that fall within the applicable date range in the db? 如何仅选择数据库中适用日期范围内的代码/优惠券?

Does that make sense? 那有意义吗?

The SQL statement to select values between two dates would be something along these lines: 用于在两个日期之间选择值的SQL语句将遵循以下原则:

SELECT code 
FROM coupons 
WHERE title = 'Booyah' 
AND CURRENT_DATE BETWEEN start_date and exp_date;

I'm sure you can make the appropriate substitutions for CodeIgniter. 我确定您可以对CodeIgniter进行适当的替换。

AKA, if the start_date is less than today, don't get it, and if today is after the exp_date don't get it. 又说,如果start_date小于今天,则不​​要获取,如果今天是在exp_date之后,则不要获取。

But I'm assuming you meant "EG, if today is less than start_date, don't get it, and if today is greater than exp_date, don't get it." 但是我假设您的意思是“例如,EG,如果今天小于start_date,请不要获取;如果今天大于exp_date,请不要获取。”

Here you go: $today = today's date. 您在这里:$ today =今天的日期。

$today = date();    
$pass_check = $this->CI->db->select('code')->from('coupons')->where(array('title'=>'Booyah'))->$this->db->where("$date BETWEEN start_date and exp_date")->get();
$pass_check = $pass_check->result_array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM