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

时间:2015-10-19 05:30:32

标签: php mysql cakephp cakephp-2.0

您好我想通过本周,本月,今年来搜索MySql表格中的数据。 以下是我的控制器代码。 我没有使用当前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
            );

enter image description here

2 个答案:

答案 0 :(得分:5)

您可以使用StartDateEndDate轻松完成。添加像这样的条件

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

现在问题如何获得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()); 

本月获得

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

今年获得

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

详细信息TimeHelper Doc

没有TimeHelper

本周获得

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

本月获得

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

今年获得

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

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

过去7天

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

过去30天

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

过去1年

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

我认为现在您可以轻松解决您的问题。

答案 1 :(得分:0)

您可以使用以下条件

//current month
$conditions = array('MONTH(Model.field) = MONTH(CURDATE())', 'YEAR(Model.field) = YEAR(CURDATE())');

//current week
$conditions = array('YEARWEEK(Model.field,1) = YEARWEEK(CURDATE(), 1)');
//current year
$conditions = array('YEAR(Model.field) = YEAR(CURDATE())');