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
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"
}