Skip to content

Instantly share code, notes, and snippets.

@maietta
Created July 10, 2017 12:07
Show Gist options
  • Save maietta/782366a7835ff030ecc56deaf7baa6f3 to your computer and use it in GitHub Desktop.
Save maietta/782366a7835ff030ecc56deaf7baa6f3 to your computer and use it in GitHub Desktop.
Decades with years by range of Years (PHP)
/* To get a list of decades with the years, from a range selection. Useful for navigational elements on webpages, for example. */
Array
(
[2010] => Array
(
[0] => 2018
[1] => 2017
[2] => 2016
[3] => 2015
[4] => 2014
[5] => 2013
[6] => 2012
[7] => 2011
[8] => 2010
)
[2000] => Array
(
[0] => 2009
[1] => 2008
[2] => 2007
[3] => 2006
[4] => 2005
[5] => 2004
[6] => 2003
[7] => 2002
[8] => 2001
[9] => 2000
)
....
)
*/
$decades = decades_from_year(1972, 2018);
function decades_from_year($start_year, $end_year) {
$decades = array();
foreach(range($end_year, $start_year) as $year) {
$decades[substr(($year), 0, -1)."0"][] = $year;
}
return $decades;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment