get startdate and enddate for current quarter php

前端 未结 19 1696
悲哀的现实
悲哀的现实 2020-12-29 10:58

I am trying to set a start date and end date by the quarter.

For example, I am working on a reporting system where i need to report data for quarter 1, quarter 2, qu

19条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 11:22

    I needed some function to give me dates for each quarter so I could fetch some sales info. Here is the code if anyone needs it:

       private function getQuarterRange($quarter, $year=null) {
            if ($quarter > 4 || $quarter < 1)
                return null;
    
            if ($year == null)
                $year = date('Y');
    
    
            $startDate = date('Y-m-d', strtotime($year . '-' . (($quarter * 3) - 2). '-1'));
            $endDate = date('Y-m-d', strtotime(date('Y-m-d', strtotime($startDate)) . '+3 month - 1 day'));
            return ['startDate' => $startDate, 'endDate' => $endDate];
        }
    

    Simply call: var_dump($this->getQuarterRange(4, 2018)); Output:

    array(2) {
      ["startDate"]=>
      string(10) "2018-10-01"
      ["endDate"]=>
      string(10) "2018-12-31"
    }
    

提交回复
热议问题