Last active
December 21, 2021 20:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was very helpful to me