Skip to content

Instantly share code, notes, and snippets.

@steveirl
Last active December 21, 2021 20:09
Show Gist options
  • Save steveirl/63c65561b6000de5c346 to your computer and use it in GitHub Desktop.
Save steveirl/63c65561b6000de5c346 to your computer and use it in GitHub Desktop.
Get a list of (date) quarters between two given dates. Return array of objects with information about each quarter
<?php
// get month name from number
function month_name($month_number){
return date('F', mktime(0, 0, 0, $month_number, 10));
}
// get get last date of given month (of year)
function month_end_date($year, $month_number){
return date("t", strtotime("$year-$month_number-0"));
}
// return two digit month or day, e.g. 04 - April
function zero_pad($number){
if($number < 10)
return "0$number";
return "$number";
}
// Return quarters between tow dates. Array of objects
function get_quarters($start_date, $end_date){
$quarters = array();
$start_month = date( 'm', strtotime($start_date) );
$start_year = date( 'Y', strtotime($start_date) );
$end_month = date( 'm', strtotime($end_date) );
$end_year = date( 'Y', strtotime($end_date) );
$start_quarter = ceil($start_month/3);
$end_quarter = ceil($end_month/3);
$quarter = $start_quarter; // variable to track current quarter
// Loop over years and quarters to create array
for( $y = $start_year; $y <= $end_year; $y++ ){
if($y == $end_year)
$max_qtr = $end_quarter;
else
$max_qtr = 4;
for($q=$quarter; $q<=$max_qtr; $q++){
$current_quarter = new stdClass();
$end_month_num = zero_pad($q * 3);
$start_month_num = ($end_month_num - 2);
$q_start_month = month_name($start_month_num);
$q_end_month = month_name($end_month_num);
$current_quarter->period = "Qtr $q ($q_start_month - $q_end_month) $y";
$current_quarter->period_start = "$y-$start_month_num-01"; // yyyy-mm-dd
$current_quarter->period_end = "$y-$end_month_num-" . month_end_date($y, $end_month_num);
$quarters[] = $current_quarter;
unset($current_quarter);
}
$quarter = 1; // reset to 1 for next year
}
return $quarters;
}
$quarters = get_quarters('2010-04-05', '2015-05-01');
// Test
echo '<pre>';
print_r($quarters);
echo '</pre>';
@waqas83
Copy link

waqas83 commented Jun 3, 2016

The function is very helpful. However, it needs some adjustments.
It does not work for following dates:
$quarters = get_quarters('2015-03-31', '2016-04-14');
It doesn't calculate the "period_end" correctly. Observe the [period_end] => 2015-03-28 which is not the end date of march.
To correct this problem please change the 0 to 1 in following

function month_end_date($year, $month_number){
return date("t", strtotime("$year-$month_number-0"));
}

so the correct function would be:

function month_end_date($year, $month_number){
return date("t", strtotime("$year-$month_number-1"));
}

Hope it helps somebody.

@dheerajkukreja
Copy link

Thanks i was really in need of this

@abuujurayj
Copy link

Was very helpful to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment