Forum Moderators: coopster

Message Too Old, No Replies

Loop construct problem

showing stats for a range of months.

         

Jaze

9:10 am on Mar 17, 2004 (gmt 0)

10+ Year Member



I've fallen back to beginner level, could someone please help me to get this script to display all the months involved. I'm querying the months and years from a database, ie field Months with values 1 thru 12 and Years, values 2001 thru to recent year. Perhaps not the ideal setup but not able to change it easily.

<HTML>
<HEAD>
<TITLE>Display each month from stat month,stat year to tomonth,toyear</TITLE>
</HEAD>

<BODY>
<?php
$tomonth=2;
$toyear=2004;

$stat_start_month=10;
$stat_start_year=2002;

$stop_stats=0;

while (($stat_start_month!=$tomonth) && ($stat_start_year!=$toyear)){
if ($stat_start_month==13){
$stat_start_year++;
$stat_start_month=1;
} else {
$stat_start_month++;
}
print $stat_start_month." ".$stat_start_year."<br>";
$stop_stats++;
}
?>
</BODY>
</HTML>

coopster

1:07 pm on Mar 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could use a combination of the Date and Time Functions [php.net] available in PHP:
$start_month = '10'; 
$start_year = '2002';
$end_month = date [php.net]('m'); // current month MM
$end_year = date [php.net]('Y'); // current year YYYY
for [php.net] (
$year_month = $start_year . $start_month;
$year_month <= $end_year . $end_month;
$year_month =
strftime [php.net]('%Y%m',mktime [php.net](0,0,0,substr [php.net]($year_month,4,2)+1,1,substr [php.net]($year_month,0,4)))
) {
print substr [php.net]($year_month,4,2) . ' ' . substr [php.net]($year_month,0,4) . '<br />';
}

Glacai

1:57 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



Its early for me but maybe..

while($stat_start_year <= $toyear)
{
print $stat_start_month." ".$stat_start_year."<br>";
if((++$stat_start_month) > 12)
{
$stat_start_month = 1;
$stat_start_year++;
}
}

Jaze

12:03 am on Mar 18, 2004 (gmt 0)

10+ Year Member



Thank you Glacai and Coopster,

sorry, I meant "to a specified month and year", not that I wrote that.

From your posts I have managed to get a loop working the way I wanted.

Cheers, Jaze