douxuelv7755 2015-10-19 05:30
浏览 38

如何使用本周,本月,今年的CakePHP搜索数据

Hi i want to search data from MySql table with This Week, This month, This Year.. Below is my Controller Code. I am not getting any data with Current DATE_SUB(NOW(), INTERVAL 1 WEEK

Controller initial Code:

 $condition = array('Order.eventid' => $id, 'Order.userid !=' => '');
            if (isset($_GET['dr']) && !empty($_GET['dr'])) {

                if ($_GET['dr'] == 2) { // This week 
                    $condition['Order.addeddate'] = 'DATE_SUB(NOW(), INTERVAL 1 WEEK)';
                }
                if ($_GET['dr'] == 3) { // This month 
                    $condition['Order.addeddate'] = 'DATE_SUB(NOW(), INTERVAL 1 MONTH)';
                }
                if ($_GET['dr'] == 4) { // This year 
                    $condition['Order.addeddate'] = 'DATE_SUB(NOW(), INTERVAL 1 YEAR)';
                }
                if ($_GET['dr'] == 5) { //  Custom date range 
                    //$condition[] = array('Order.addeddate' => 'DATE_SUB(NOW(), INTERVAL 1 MONTH)');
                }
            }
     if (isset($_GET['ot']) && !empty($_GET['ot'])) {
                if ($_GET['ot'] == 'attending') { //attending 
                    $condition['Order.orderstatus'] = '1';
                }
                if ($_GET['ot'] == 'processing') { //online_sales 
                    $condition['Order.orderstatus'] = '2';
                }
                if ($_GET['ot'] == 'manual') { //manual 
                    $condition['Order.orderstatus'] = '1';
                }
                if ($_GET['ot'] == 'refunded') { //refunded 
                    $condition['Order.orderstatus'] = '1';
                }
            }
    $this->paginate = array(
                'conditions' => $condition
            );

enter image description here

  • 写回答

2条回答 默认 最新

  • dongren2128 2015-10-19 07:18
    关注

    You can do it easily by use StartDate and EndDate. Add the condition like this

    'conditions' => array('date(YourModel.addeddate) BETWEEN ? AND ?' => array($sdate,$edate)
    

    Now the question how you will get this month this week this year. You can use this below code.

    You can use cakephp TimeHelper

    By using cakephp TimeHelper

    For get this week

    $sdate = $this->Time->format('y-m-d','last saturday');
    $edate = $this->Time->format('y-m-d',time()); 
    

    For get this month

    $sdate = $this->Time->format('y-m-01',time());
    $edate = $this->Time->format('y-m-d',time());  
    

    For get this year

    $sdate = $this->Time->format('y-01-01',time());
    $edate = $this->Time->format('y-m-d',time()); 
    

    For details TimeHelper Doc

    Without TimeHelper

    For get this week

    $sdate = date('y-m-d', strtotime("last saturday"));
    $edate = date("y-m-d"); 
    

    For get this month

    $sdate = date('y-m-01');
    $edate = date("y-m-d"); 
    

    For get this year

    $sdate = date('y-01-01');
    $edate = date("y-m-d"); 
    

    Now if you want to get last 7 days or last 30 days, you can use strtotime.

    For get last 7 days

    $sdate = date('Y-m-d', strtotime("-1 weeks"));
    $edate = date("y-m-d"); 
    

    For get last 30 days

    $sdate = date('Y-m-d', strtotime("-30 days"));
    $edate = date("y-m-d");
    

    For get last 1 year

    $sdate = date('Y-m-d', strtotime("-1 year"));
    $edate = date("y-m-d");
    

    I think now you can easily solve your problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据