Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

generate next 12 months 1

Status
Not open for further replies.

overflo

Technical User
Feb 10, 2003
70
AU
I need a script that generates the next 12 months. i.e. if it was April it would generate April '06 to March '07. I presume I need to use jdmonthname but have no idea where to start. Any help will be greatly appreciated.
 
I don't understand what you mean by "generate the next 12 months". Do you mean "generated the monthly calendars for the 12 months following a given month"?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Sorry, I didn't explain it properly. The output is for links for a calendar of events so only month names and maybe year if possible. The output needs to look like this if it was, say, October
2006
October
November
December
2007
January
February
etc
 
I think you maybe making this more complicated than it needs to be. Since months are standard from year to year and you do not have to deal with days (at this point), I would go for a straight forward approach.
Code:
<?php
//create an array of the months
$month_list = array('January', 'February', 'March',
                    'April'  , 'May'      , 'June'  ,
                    'July'   , 'August'   , 'September',
                    'October', 'November' , 'December');

//Get the current month as an integer
//and the current year as a four digit integer.
//Assign them to $month and $year variables
//These variables can be set from user input 
//or as in this case from PHP.

[url=http://us3.php.net/manual/en/function.list.php]list[/url]($month, $year) = [url=http://us3.php.net/manual/en/function.explode.php]explode[/url](':',[url=http://us3.php.net/manual/en/function.date.php]date[/url]('n:Y'));

//array_slice($month_list, $month-1) = an array starting 
//with '$month' and ending with December.
$current_year = [url=http://us3.php.net/manual/en/function.array-slice.php]array_slice[/url]($month_list, $month-1);

//array_slice($month_list, 0, $month-1) = an array starting 
//with 'January' and ending with the month before 
//the start month.
$next_year    = array_slice($month_list, 0, $month-1));

//To make one array of months in the proper order you can
//use array_merge()
$ordered_months = [url=http://us3.php.net/manual/en/function.array-merge.php]array_merge[/url]($current_year, $next_year);
?>
Now you have one array containing the months in the order that you would like, and you can print them to the screen. While printing use the $year variable and either check for "December", to know when to print $year+1, or you can keep the arrays separate...
 
One thing I forgot to mention, if the current month is January, then the variable $next_year will be an empty array...

Also in my code above, this line:
Code:
$next_year    = array_slice($month_list, 0, $month-1)[COLOR=red])[/color];
has an extra ')' on it, sorry.
 
Thanks. Looks like just what I need. Much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top