I am attempting to figure out how I can do the following:
Take a date from a MySQL database like: 2006-06-01 (June 1, 2006) Then have a dropdown list of remaining months using for example todays date of 2005-08-23 This would product a dropdown like:
1 month 2 months 3 months 4 months ...... 10 months
Any help on this would be GREATLY appreciated. Thanks
You can do that like this:
$beginyear; $beginmonth; $endyear; $endmonth; $years; $total_months; if ($endyear < $beginyear || ($endyear == $beginyear && $endmonth < $beginmonth)) { echo "There's a problem"; } else if ($endyear == $beginyear) { $total_months = $endmonth - $beginmonth; } else { while ($beginyear > $endyear) { $years++; $endyear--; } $total_months = ($years * 12) + ($endmonth - $begin_month); } for ($i=1;$i<=$total_months;$i++) { echo $i ." Months"; }
Where beginyear, beginmonth, endyear, endmonth are equivilent to what they describe.
Thanks for the code but I am not able to give me a list of more then 6 months.
This is what I am using:
<?PHP $beginyear = date(Y);; $beginmonth = date(m); $endyear = "2006"; $endmonth = "06"; $years; $total_months; echo " beginyear: $beginyear<BR> beginmonth: $beginmonth<BR> endyear: $endyear<BR> endmonth: $endmonth<BR> "; if ($endyear < $beginyear || ($endyear == $beginyear && $endmonth < $beginmonth)) { echo "There's a problem"; } else if ($endyear == $beginyear) { $total_months = $endmonth - $beginmonth; } else { while ($beginyear > $endyear) { $years++; $endyear--; } $total_months = ($years * 12) + ($endmonth - $begin_month); } for ($i=1;$i<=$total_months;$i++) { echo $i ." Months "; } ?>
This is what it shows in the browser:
beginyear: 2005 beginmonth: 08 endyear: 2006 endmonth: 06 1 Months 2 Months 3 Months 4 Months 5 Months 6 Months
Oh, sorry, two little bugs in that:
In the while loop, it should be $endyear > $beginyear, not the other way around.
At the end of the else {, it should be $beginmonth, not $begin_month
I've tested that, and it seems to work.
Thanks alot for your assistance. It works great now.
No problem. Glad it works!