Forum Moderators: coopster
<?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
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
monthportion of the date. Make sense?
1you must take into account leap years, if we were using February, for example
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 />';
}