Issue
How to apply date range filters to get rows of exact date
Below is my sql table
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
But when i select date 2018/07/01 - 2018/07/01 it returns empty rows,
and when i select date 2018/07/01 - 2018/07/02 then it will shows below single row
2 test 2018-07-01 10:30:45
My query is, how to apply date filter if i need record of selected date in from and to date range filter
Below is my php code
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);
}
}
}
Solution
You have to add time also in query
if(!empty($frmDate)){
$conditions['Orders.created >='] = $frmDate.' 00:00:00';
}
if(!empty($toDate)){
$conditions['Orders.created <='] = $toDate.' 23:59:59';
}
Answered By - B. Desai
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.