0

I want to create a dynamic array that will look like this, using years:

array(2012,2011,2010,2009,2008,2007,2006,2005)

Basically I have the starting year which is constant, 2005, and I want the array to dynamically change each year, adding the new year. Currently, I grab the current year via:

$this_year = date('Y');

Is there some easy way to generate the array using the two known years, the constant 2005 and the current year?

Thanks for taking a look.

2
  • This search returns some related questions which would have helped you as well. Commented Mar 9, 2012 at 9:11
  • I was thinking more in terms of sequential numbers as oppose to years, I guess it's obvious that people would have tried this before specifically for years. Commented Mar 9, 2012 at 9:13

4 Answers 4

6

$years = range(CONSTANT_YEAR, date('Y'));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, figured it was easy. Lol, took me way to long to realize this generates an array and that I don't have to do array($years).. it's getting late, long day :)
1

http://php.net/manual/en/function.range.php

have you tried this?

Comments

1

You could do something like this

$this_year = date("Y");
$years_array = array();
for ($i == 2005; $i < $this_year; $i++) {
array_push($years_array,$i);
}
print_r $years_array;

You use a for cycle that starts in 2005 and stops in current year, for each year it will add it to the end of the array, I haven't tested it but it should work

Comments

1

Here it is http://php.net/manual/en/function.range.php $years = range(2005,date('Y'));

2 Comments

Just curious, what is the point of the (int)? Does it need it?
date() returns a string, range() takes mixed; thus no conversion needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.