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

Puz*_*Boy 0 php mysql cakephp cakephp-2.0

嗨我想用本周,本月,今年从MySql表中搜索数据.下面是我的控制器代码.我没有使用Current获取任何数据DATE_SUB(NOW(), INTERVAL 1 WEEK

控制器初始代码:

 $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
            );
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ali*_*rim 5

您可以使用StartDate和轻松完成EndDate.添加这样的条件

'conditions' => array('date(YourModel.addeddate) BETWEEN ? AND ?' => array($sdate,$edate)
Run Code Online (Sandbox Code Playgroud)

现在问你将如何得到this month this week this year.您可以使用以下代码.

您可以使用 cakephp TimeHelper

通过使用cakephp TimeHelper

本周获得

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

为了这个月

$sdate = $this->Time->format('y-m-01',time());
$edate = $this->Time->format('y-m-d',time());  
Run Code Online (Sandbox Code Playgroud)

为今年获得

$sdate = $this->Time->format('y-01-01',time());
$edate = $this->Time->format('y-m-d',time()); 
Run Code Online (Sandbox Code Playgroud)

有关详细信息TimeHelper Doc

没有TimeHelper

本周获得

$sdate = date('y-m-d', strtotime("last saturday"));
$edate = date("y-m-d"); 
Run Code Online (Sandbox Code Playgroud)

为了这个月

$sdate = date('y-m-01');
$edate = date("y-m-d"); 
Run Code Online (Sandbox Code Playgroud)

为今年获得

$sdate = date('y-01-01');
$edate = date("y-m-d"); 
Run Code Online (Sandbox Code Playgroud)

现在,如果您想要过去7天或过去30天,您可以使用strtotime.

过去7天

$sdate = date('Y-m-d', strtotime("-1 weeks"));
$edate = date("y-m-d"); 
Run Code Online (Sandbox Code Playgroud)

过去30天

$sdate = date('Y-m-d', strtotime("-30 days"));
$edate = date("y-m-d");
Run Code Online (Sandbox Code Playgroud)

为了获得最后1年

$sdate = date('Y-m-d', strtotime("-1 year"));
$edate = date("y-m-d");
Run Code Online (Sandbox Code Playgroud)

我想现在你可以轻松解决你的问题了.