PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] How to apply date filters in reports php

 January 16, 2022     cakephp, cakephp-3.0, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing