Forum Moderators: coopster

Message Too Old, No Replies

getdate this and next month etc.

         

tristeve

3:06 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



Just looked at this old post on the forum [webmasterworld.com...]
and am using some of the ideas to try to retrieve a list of this month, next month, etc.
My code at the moment (which was working yesterday) is:

<?php
$month = date ("F");
echo ($month);
$next = date ("F", strtotime ("+1 month"));
echo ($next);
$next2 = date ("F", strtotime ("+2 month"));
echo ($next2);
$next3 = date ("F", strtotime ("+3 month"));
echo ($next3);
$next4 = date ("F", strtotime ("+4 month"));
echo ($next4);
$next5 = date ("F", strtotime ("+5 month"));
echo ($next5);
$next6 = date ("F", strtotime ("+6 month"));
echo ($next6);
$next7 = date ("F", strtotime ("+7 month"));
echo ($next7);
$next8 = date ("F", strtotime ("+8 month"));
echo ($next8);
$next9 = date ("F", strtotime ("+9 month"));
echo ($next9);
$next10 = date ("F", strtotime ("+10 month"));
echo ($next10);
$next11 = date ("F", strtotime ("+11 month"));
echo ($next11);
?>

Today however, my list of months is output as follows:
March, May, May, July, July, August, October, October, December, December, January, March.

I know it's not pretty, but can anyone see where I'm going wrong.
Yesterday the output was March, April, May, June, July, August, September, October, November, December, January, February

Thanks in advance,
Steve

coopster

3:37 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, tristeve!

The reason this is happening is that one month from today, March 31, is April 31. But since there is no April 31 this year1 the date is determined to be May 1. And you are only returning the

month
portion of the date. Make sense?

1you must take into account leap years, if we were using February, for example

tristeve

3:43 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



Makes perfect sense. Thanks Coopster for the quick response.

coopster

4:57 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure. Here are a couple of code snippets to help you get the next month as you would like. I included links to the PHP manual that describes the functions being used.
print 'The months, starting with this month:<br />'; 
for ($i = 0; $i < 12; $i++) {
print date [php.net]("F", mktime [php.net](0, 0, 0, date('m')+$i, 1, date('Y')));
print '<br />';
}
print 'The months, starting with next month:<br />';
for ($i = 0; $i < 12; $i++) {
print date('F', strtotime [php.net]("+$i month", strtotime('first day')));
print '<br />';
}