如何在报告中应用日期过滤器 php

How to apply date filters in reports php

如何应用日期范围过滤器来获取准确日期的行

下面是我的sqltable

id  name     created
1   abc   2018-07-06 10:30:45 
2   test  2018-07-01 10:30:45  
3   raj   2018-07-05 10:30:45  
4   zyz   2018-07-08 10:30:45

但是当我 select 日期 2018/07/01 - 2018/07/01 它 returns 空行,

当我 select 日期为 2018/07/01 - 2018/07/02 时,它将显示在单行下方

2   test  2018-07-01 10:30:45 

我的问题是,如果我需要在开始和结束日期范围过滤器

中记录 selected 日期,如何应用日期过滤器

下面是我的php代码

public function clientReport(){
        if ($this->request->is('post')) {
            $daterng = $this->request->data['daterange'];
            if(!empty($daterng)){
                $dates = explode("-", $daterng);
                $frmDate =  date("Y-m-d", strtotime($dates[0]));
                $toDate =  date("Y-m-d", strtotime($dates[1]));
            } 
            $conditions = [];
            if(!empty($frmDate)){
                $conditions['Orders.created >='] = $frmDate;
            }
            if(!empty($toDate)){
                $conditions['Orders.created <='] = $toDate;
            }
            $orders = $order
            ->find() 
            ->where($conditions);            
            $orders->enableHydration(false);  //  You can retrieve basic arrays by disabling hydration 
            if(!empty($orders)){
              $this->set('clientlist', $orders);
            }

        }        
    }

您还必须在查询中添加时间

       if(!empty($frmDate)){
            $conditions['Orders.created >='] = $frmDate.' 00:00:00';
        }
        if(!empty($toDate)){
            $conditions['Orders.created <='] = $toDate.' 23:59:59';
        }

要根据日期范围过滤您的数据库,您可以使用此

SELECT * FROM your_table WHERE DATE(created) BETWEEN '2018-07-01' AND '2018-07-01'

您需要将结束日期 "round up" 设为 23:59:59 小时。您实际上是在使用时间戳,因此日期将被转换为 date/time。 2018/07/01 那天大概是午夜00:00:00。搜索2018/07/01到2018/07/01时,其实你要搜索的是2018/07/0100:00:00到2018/07/0123:59:59.