如何在php codeigniter中按选定日期过滤记录?

h6my8fg2  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(316)

我试着用客户的报告画一个图表,我想过滤掉由于非商业日而关闭的商店的多天。我还有一个过滤日期从和到。

if( $dateFrom != null ) :
        $dateFrom .= ' 00:00:01';
        $this->db->where('created >=', $dateFrom);
    endif;
    if( $dateTo != null ) :
        $dateTo .= ' 23:59:59';
        $this->db->where('created <=', $dateTo);
    endif;

我想知道如何添加一个多天的额外过滤器。所以我现在有这样的东西,但它不起作用。

$this->db->where("created BETWEEN '2020-01-01 00:00:01' AND '2020-01-25 23:59:59'"); 
$this->db->where("created BETWEEN '2020-01-26 00:00:01' AND '2020-04-05 23:59:59'"); 
$this->db->where("created BETWEEN '2020-04-06 00:00:01' AND '2020-04-25 23:59:59'");
$this->db->where("created BETWEEN '2020-04-26 00:00:01' AND '2020-06-27 23:59:59'");
$this->db->where("created BETWEEN '2020-06-28 00:00:01' AND '2020-08-29 23:59:59'");
$this->db->where("created BETWEEN '2020-08-30 00:00:01' AND '2020-12-12 23:59:59'");
$this->db->where("created BETWEEN '2020-12-13 00:00:01' AND '2020-12-19 23:59:59'");
$this->db->where("created BETWEEN '2020-12-20 00:00:01' AND '2020-12-31 23:59:59'");

所以我只是想弄清楚如何从一个日期和一个日期中筛选出一个额外的天数。
是否有解决方法或任何修复?
谢谢!

u3r8eeie

u3r8eeie1#

您可以将所有例外日期放入如下数组:

$exceptions = ['2020-04-26', '2020-04-27']; // And so on

然后捕获/定义您的日期范围:

$dateFrom = $some_source_of_data;
$dateTo = $some_source_of_data2;

然后:

$this->db->where("DATE(created) BETWEEN DATE('{$dateFrom}') AND DATE('{$dateTo}')"); 
$this->db->where("DATE(created) NOT IN (".implode(',', $exceptions).")");
c0vxltue

c0vxltue2#

我自己想办法。我加上“不在中间”,然后加上我想过滤掉的日子。

相关问题