Okay, here is a PHP function that accomplishes this. It calculates the target end year (current year - 5), and then iterates backward month by month from the current month/year until it reaches January of the target year.
<?php
/**
* Generates an array of "month/year" strings going backward 5 years
* from the current month/year, ending in January of the 5-year-back year.
*
* @return array An array of strings in the format "month/year" (e.g., ["10/2023", "9/2023", ..., "1/2018"]).
*/
function getFiveYearsBackMonths() {
$monthsArray = [];
// Get current month and year
$currentMonth = (int)date('n'); // 'n' gives month without leading zero (1-12)
$currentYear = (int)date('Y'); // 'Y' gives four-digit year
// Calculate the target end year (5 years ago)
$endYear = $currentYear - 5;
// Start iteration from the current month and year
$month = $currentMonth;
$year = $currentYear;
// Loop until we go past January of the end year
// The condition continues as long as the current year is after the end year,
// OR if it's the end year but the month is still 1 or greater.
while ($year > $endYear || ($year == $endYear && $month >= 1)) {
// Add the current month/year string to the array
// Month is already without leading zero from the calculation logic
$monthsArray[] = $month . '/' . $year;
// Move to the previous month
$month--;
// If the month becomes 0, it means we've gone from January (1)
// We need to wrap around to December (12) and decrement the year
if ($month == 0) {
$month = 12; // Go to December
$year--; // Decrement the year
}
// The loop condition ($year > $endYear || ($year == $endYear && $month >= 1))
// correctly handles stopping after processing 1/endYear.
// For example, if endYear is 2018, when we process 1/2018, month becomes 0, year becomes 2017.
// The next iteration condition (2017 > 2018 || (2017 == 2018 && 0 >= 1)) is false, and the loop stops.
}
return $monthsArray;
}
// --- Example Usage ---
$fiveYearsOfMonths = getFiveYearsBackMonths();
// Print the results (for demonstration)
echo "Months backward from current month for 5 years, ending Jan of that year:\n";
print_r($fiveYearsOfMonths);
// You can also loop through them:
/*
foreach ($fiveYearsOfMonths as $my) {
echo $my . "\n";
}
*/
?>
Explanation:
getFiveYearsBackMonths() function: Defines the function.
$monthsArray = [];: Initializes an empty array to store the results.
$currentMonth = (int)date('n');: Gets the current month as a number (1-12) without leading zeros using date('n'). Casts to int for safety.
$currentYear = (int)date('Y');: Gets the current four-digit year using date('Y'). Casts to int.
$endYear = $currentYear - 5;: Calculates the target year, which is 5 years before the current year.
$month = $currentMonth; $year = $currentYear;: Initializes variables that will track the current month and year within the loop, starting from the present.
while ($year > $endYear || ($year == $endYear && $month >= 1)): This is the core loop condition:
$year > $endYear: Continue as long as the current year is greater than the target end year.
||: OR
($year == $endYear && $month >= 1): Continue if the current year is the target end year, and the month is January (1) or greater. This ensures we include January of the target year.
$monthsArray[] = $month . '/' . $year;: Inside the loop, formats the current $month and $year into the desired "month/year" string and adds it to the results array.
$month--;: Decrements the month.
if ($month == 0) { ... }: Checks if decrementing the month resulted in 0. This happens when we were in January (1).
- If it's 0, it means we need to go to the previous year.
$month = 12;: Set the month to December.
$year--;: Decrement the year.
- The loop continues until the condition is no longer met (i.e., when
$year is less than $endYear, or when $year is $endYear but $month has gone below 1).
return $monthsArray;: Returns the populated array.