PDA

View Full Version : looping the last year


gavimobile
07-31-2010, 06:49 PM
hey folks,

its been a while since my last post, i cannot seem to get my logic right.

im trying to loop each month (12 months) from the last day in this month from last year.


//what is the last day in this month and this year
$days_this_month = cal_days_in_month(CAL_GREGORIAN, date('n'), date('Y'));
//lets change it to a regular date which mysql can use eg. 2010-07-31
$last_day_this_month = date('Y-m-'.$days_this_month);

//this is the last day in this month this year
echo $last_day_this_month;

//now i will subtract 1 year from this date
$lastYear = strtotime('-1 Year', strtotime($this_month));
$lastYear = date('Y-m-d', $lastYear );

//that should echo 2009-07-31

this is where im stuck. im not sure how i would do the loop
i need it to output the last day of every month for the last year starting from 1 year ago from the last day of the current month till the last day of the current month

should output like this

2009-07-31
2009-08-31
2009-09-30
2009-10-31
2009-11-30
2009-12-31
2010-01-31
2010-02-28
2010-03-31
2010-04-30
2010-05-31
2010-06-30
2010-07-31


thanks in advance

gavimobile
08-01-2010, 05:30 PM
waiting eagerly for your responses please!

gavimobile
08-02-2010, 05:49 PM
well tonight my brain is working.. for thoese of you who stumbled upon this post here is what i did. sorry i dont have time to explain it right now, ill try to edit it later.

for ($i=date('n'); $i <= 12; $i++){
if ($i == 12 && empty($stop)){
echo '<tr><td>'.$i.'</td></tr>';
$i = 1;
$stop = '%@#';
}
if (isset($stop)){
if (date('n') > $i){
echo '<tr><td>'.$i.'</td></tr>';
}
}else{
echo '<tr><td>'.$i.'</td></tr>';
}

}

gavimobile
08-04-2010, 10:40 PM
so i changed it, this is what i did in the end. i hope this will help someone



echo '<table cellpadding="2" cellspacing="2" border="2">';
echo '<tr>';

//lets refer to month as a number eg. july = 7 and not 07
//we will need it in this format to do our loops
$i=date('n');

//subtract 1 year from last year
$lastYear = strtotime('-1 Year', strtotime(date('Y-m-d')));
//this will change it to a regular looking date
$lastYear = date('Y', $lastYear );

//lets start looping all numbers from this month till december eg. 7-12. july till december
while ($i <= 12 ){

//each time the loop goes, find the last day of each month ($i)
$days_this_month = cal_days_in_month(CAL_GREGORIAN, $i, $lastYear);

//we will add a 0 in front of $i to make is 2 digits for 1 digit months, eg jan-sept are 1 digin months
//oct-dec are 2 digit months, eg. 10, 11, 12
if (strlen($i) < 2){
$day = $lastYear.'-0'.$i.'-'.$days_this_month;
}else{
$day = $lastYear.'-'.$i.'-'.$days_this_month;
}

echo '<td>'.$day.'</td>';
//increment 1 number up after we posted printed
$i++;
}

//lets make this year the current year now
//before we were subtracting one year, but we are after the 12th month (dec)
//so we will start a new year which is this year
$changeYear = date('Y');

//start from the first month (jan) change month to one
$i=1;

//start the new loop, for all months which are less than this month
//loop till month is equal to current month
//eg. if this month is july so loop from 1-7. from jan to july
//keep in mind we will be needing to print the new year as well
while ($i <= date('n') ){

//get the last day of the month once again using this year.
//this should prevent any trouble with leapyears etc.
$days_this_month = cal_days_in_month(CAL_GREGORIAN, $i, $changeYear);
//once again, we will add a 0 in front of $i to make is 2 digits for 1 digit months, eg jan-sept are 1 digin months
//oct-dec are 2 digit months, eg. 10, 11, 12
if (strlen($i) < 2){
$day = $changeYear.'-0'.$i.'-'.$days_this_month;
}else{
$day = $changeYear.'-'.$i.'-'.$days_this_month;
}
echo '<td>'.$day.'</td>';
//increment 1 number up after we posted printed
$i++;
}
echo '</tr>';
echo '</table>';