如何在codeigniter中使用截止日期/到期日期获取行数?

时间:2016-06-20 17:54:48

标签: php mysql codeigniter

表名:directives

dir_id  |  directive  |  due_date |
-----------------------------------
1       |  some text  |  2017-03-30 02:00:00
2       |  some text  |  2016-04-30 02:00:00
3       |  some text  |  2017-04-30 02:00:00
4       |  some text  |  2016-03-30 02:00:00
5       |  some text  |  2015-04-30 02:00:00
6       |  some text  |  2016-04-30 02:00:00

使用三个条件。

if剩余60 days到期日显示green elseif还有5 days显示yellow else if zero(0) days or minus days已通过截止日期显示red

另外,如果可能的话,分别获得行数*You can use different query for row count*,例如60 days left = 5, data with 5 days left = 4, data with 0 days or minus = 4

'可爱的一天'

谢谢

**最终编辑“行数的工作代码”**

   //For Zero days
   $this->db->where('due_date < now()');
   $query0 = $this->db->count_all_results('directives');

   //For five days left
   $this->db->where('due_date between now() - interval 6 day and now() + interval 5 day');
   $query5 = $this->db->count_all_results('directives');

   //For more than 5+ days left
   $this->db->where('due_date > now() + interval 6 day');
   $query6 = $this->db->count_all_results('directives');

如果我没有遵循标准程序,请告诉我。

谢谢

1 个答案:

答案 0 :(得分:0)

/ <强> UNTESTED /

select case
    when due_date between now() - interval 5 day and sysdate()
       then "red"
    when due_date between now() - interval 6 day and now() - interval 59 day
       then "Yellow"
when due_date > now() + interval 60 day
       then "Green"
end as age,
sum(case when due_date between now - interval 5 day and sysdate() then 1 else 0 end) red_count 
 ///// sum others to get counts
from table