如何在yy-mm formate中获得两个日期之间的差异

时间:2016-07-20 08:08:47

标签: cakephp

我需要知道两个日期之间如何区别

App :: uses('AppController','Controller');

class ActivatednumberReportsController扩展AppController {

public $components = array('Paginator');
public $uses = array('ActivatednumberReport');

public function beforeFilter() {
    $validUser = array(1,3);
    if (!in_array($this->Auth->User('group_id'), $validUser)) {
        return $this->redirect(array('controller' => 'dashboards'));
    }
    parent::beforeFilter();
}

public function index() {
    $addCon = array();
    $fromDate = $toDate = $interval ='';

    // if(!empty($this->request->data)){
        // $fromDate = $this->request->data['ActivatednumberReport']['from_date'];
        // $toDate = $this->request->data['ActivatednumberReport']['to_date'];

    // }


    if(!empty($this->params['named'])){
       if(!empty($this->params['named']['from_date'])){
            $fromDate = $this->params['named']['from_date'];
       }

       if(!empty($this->params['named']['to_date'])){
            $toDate = $this->params['named']['to_date'];
       }
    }

    if(!empty($fromDate)){
        $fromDateArr = explode('-',$fromDate);
        $fromDate_year = $fromDateArr['0'];
        $fromDate_month = $fromDateArr['1'];
    }

    if(!empty($toDate)){
        $toDateArr = explode('-',$toDate);
        $toDate_year = $toDateArr['0'];
        $toDate_month = $toDateArr['1'];
    }


    if (!empty($fromDateArr) && !empty($toDateArr)) {
        $addCon[] = array(
            'ActivatednumberReport.year >=' => $fromDate_year,
            'ActivatednumberReport.month >=' => $fromDate_month,
            'ActivatednumberReport.year <=' => $toDate_year,
            'ActivatednumberReport.month <=' => $toDate_month,
        );
    }


    if ((!empty($fromDate) && !empty($toDate)) && (strtotime($fromDate)) > (strtotime($toDate))) {
        $this->Session->setFlash(__('PLEASE_SELECT_FROM_DATE_LESS_THEN_TO_DATE'), 'bad');
        return $this->redirect(array('action' => 'index'));
    } 


    $targetArr = $this->ActivatednumberReport->find(
       'all',array(
        'fields' => array(
         'count(ActivatednumberReport.id) as total',
         'ActivatednumberReport.status',
         'ActivatednumberReport.year',
         'ActivatednumberReport.month',
        ),
        'conditions' => array(
         'ActivatednumberReport.status' => array(2,3),
         'ActivatednumberReport.reactivted' => 1,$addCon
        ),
        'group' => array('ActivatednumberReport.month','ActivatednumberReport.status')
       )
    );

    $reactivtedArr = array();
    if(!empty($targetArr)){
        foreach($targetArr as $rItem){
            if($rItem['ActivatednumberReport']['status'] == 2){
                $reactivtedArr[$rItem['ActivatednumberReport']['month']]['ISSL'] = $rItem[0]['total'];
            }else{
                $reactivtedArr[$rItem['ActivatednumberReport']['month']]['GP'] = $rItem[0]['total'];
            }

        }

    }
    // pr($reactivtedArr);exit;
    $this->set(compact('reactivtedArr','fromDate','toDate'));
}

//Find the doctor wise report download function
public function downloadactivatednumber($fromDate = null, $toDate = null) {
     $addCon = array();
    $fromDate = $toDate = date('Y-m');

    if(!empty($this->params['pass'])){
       if(!empty($this->params['pass']['0'])){
            $fromDate = $this->params['pass']['0'];
       }

       if(!empty($this->params['pass']['1'])){
            $toDate = $this->params['pass']['1'];
       }
    }

    if(!empty($fromDate)){
        $fromDateArr = explode('-',$fromDate);
        $fromDate_year = $fromDateArr['0'];
        $fromDate_month = $fromDateArr['1'];
    }

    if(!empty($toDate)){
        $toDateArr = explode('-',$toDate);
        $toDate_year = $toDateArr['0'];
        $toDate_month = $toDateArr['1'];
    }

    if (!empty($fromDateArr) && !empty($toDateArr)) {
        $addCon[] = array(
            'ActivatednumberReport.year >=' => $fromDate_year,
            'ActivatednumberReport.month >=' => $fromDate_month,
            'ActivatednumberReport.year <=' => $toDate_year,
            'ActivatednumberReport.month <=' => $toDate_month,
        );
    }

    $finalArr = $this->ActivatednumberReport->find('all', array('conditions' => array('ActivatednumberReport.archive' => 1,$addCon)));

    $dataArr = array();
    if (!empty($finalArr)) {
        $monthName = '';
        foreach ($finalArr as $key => $item) {
            $dateObj   = DateTime::createFromFormat('!m', $item['Customer']['month']);
            $monthName = $dateObj->format('F');

            $dataArr[$key]['msisdn'] = $item['ArchiveReport']['msisdn'];
            $dataArr[$key]['customer_id'] = $item['ArchiveReport']['customer_id'];
            $dataArr[$key]['reference_number'] = $item['ArchiveReport']['reference_number'];
            $dataArr[$key]['address_1'] = $item['ArchiveReport']['address_1'];
            $dataArr[$key]['address_2'] = $item['ArchiveReport']['address_2'];
            $dataArr[$key]['address_3'] = $item['ArchiveReport']['address_3'];
            $dataArr[$key]['address_4'] = $item['ArchiveReport']['address_4'];
            $dataArr[$key]['address_5'] = $item['ArchiveReport']['address_5'];
            $dataArr[$key]['pd_due'] = $item['ArchiveReport']['pd_due'];
            $dataArr[$key]['billcycle'] = $item['ArchiveReport']['billcycle'];
            $dataArr[$key]['csactivated'] = $item['ArchiveReport']['csactivated'];
            $dataArr[$key]['csdeactivated'] = $item['ArchiveReport']['csdeactivated'];
            $dataArr[$key]['vip_tag'] = $item['ArchiveReport']['vip_tag'];
            $dataArr[$key]['fnf_record_status'] = $item['ArchiveReport']['fnf_record_status'];
            $dataArr[$key]['year'] = $item['ArchiveReport']['year'];
            $dataArr[$key]['month'] = $monthName;
            $dataArr[$key]['status'] = $item['ArchiveReport']['status'];
        }
    }

    //Call the csv download plugins
    $_serialize = 'dataArr';
    $_header = array('Year','Month','MSISDN', 'Customer ID','Reference Number','Address_1','Address_2','Address_3','Address_4','Address_5','Pd Due','Cycle','CsActive','CsDeactive','Vip Tag','status','Fnf status');
    $_extract = array('year', 'month','msisdn', 'customer_id', 'reference_number', 'address_1', 'address_2', 'address_3', 'address_4', 'address_5', 'pd_due', 'billcycle', 'csactivated', 'csdeactivated', 'vip_tag', 'status','fnf_record_status');
    $fileName = 'Activated-Number-Report-' . time();
    $this->response->download($fileName . '.csv'); // <= setting the file name
    $this->viewClass = 'CsvView.Csv';
    $this->set(compact('dataArr', '_serialize', '_header', '_extract'));
}

} 我如何从fromDate和toDate获得差异,其中date formate = 2016-03

1 个答案:

答案 0 :(得分:0)

我不被评论。因此,我必须这样做作为答案。 我认为这个问题已经回答了: https://stackoverflow.com/a/676828/3963566