Jump to content

Recommended Posts

Just to make things a lot simpler, I decided to start a new topic.

 

My question is, how can I count how many months it is back to the past November?

 

Example: May 2011 back to November would return 6.

 

Example: Dec 2011 back to November would return 1.

 

Example: October 2011 back to November would return 11.

 

Jake

Ok, I think I'm getting somewhere now.  Now I need to total the columns that are between $MLYTDstart and $MLYTDend, store it at variable $MLYTD.  And total the columns between $MCYTDstart and $MCYTDend, store it at variable $MCYTD.

 

I am not sure how to do this.  I have the columns in variables $M1 - $M27.

 

Do I need to do some kind of for loop or while loop and append the numbers to $M to total up the range?

while (odbc_fetch_row($rs))
  {
$M1 += odbc_result($rs,"SALES_OLDEST_PD1");
$M2 += odbc_result($rs,"SALES_PD_2");
$M3 += odbc_result($rs,"SALES_PD_3");
$M4 += odbc_result($rs,"SALES_PD_4");
$M5 += odbc_result($rs,"SALES_PD_5");
$M6 += odbc_result($rs,"SALES_PD_6");
$M7 += odbc_result($rs,"SALES_PD_7");
$mate += odbc_result($rs,"SALES_PD_8");
$M9 += odbc_result($rs,"SALES_PD_9");
$M10 += odbc_result($rs,"SALES_PD_10");
$M11 += odbc_result($rs,"SALES_PD_11");
$M12 += odbc_result($rs,"SALES_PD_12");
$M13 += odbc_result($rs,"SALES_PD_13");
$M14 += odbc_result($rs,"SALES_PD_14");
$M15 += odbc_result($rs,"SALES_PD_15");
$M16 += odbc_result($rs,"SALES_PD_16");
$M17 += odbc_result($rs,"SALES_PD_17");
$M18 += odbc_result($rs,"SALES_PD_18");
$M19 += odbc_result($rs,"SALES_PD_19");
$M20 += odbc_result($rs,"SALES_PD_20");
$M21 += odbc_result($rs,"SALES_PD_21");
$M22 += odbc_result($rs,"SALES_PD_22");
$M23 += odbc_result($rs,"SALES_PD_23");
$M24 += odbc_result($rs,"SALES_PD_24");
$M25 += odbc_result($rs,"SALES_PD_25");
$M26 += odbc_result($rs,"SALES_LAST_PD_26");
$M27 += odbc_result($rs,"SALES_CURR_PD_27");
  }
  	  
$MCYTDstart=27-$backtoNovember;
$MCYTDend=27;

$MLYTDstart=$MCYTDstart - 12;
$MLYTDend=$MLYTDstart + $backtoNovember;

 

In a round about way, this is what I'm trying to do. Append the numbers between the $start and $end variables to the "$M". Loop incrementing and totaling those odbc variables.

while(loop $backtoNovember"7 times") {
$MCYTD += append $M to numbers $MCYTDstart through $MCYTDend in order to total up the variables;
}

 

So, this would output something like.

 

$MCYTD = $M21 + $MCYTD;

$MCYTD = $M22 + $MCYTD;

$MCYTD = $M23 + $MCYTD;

$MCYTD = $M24 + $MCYTD;

$MCYTD = $M25 + $MCYTD;

$MCYTD = $M26 + $MCYTD;

$MCYTD = $M27 + $MCYTD;

_________________

 

Giving me the total Current Year to Date Sales. Which is stored in $MCYTD.

How about something like this?

 

$curEnd = 27;	// 27
$curStart = 27 - abs(11-date('m'));	// 27 - 6 = 21

$prevEnd = $curStart - 1;	// 21 - 1 = 20
$prevStart = $prevEnd - 11;	// 20 - 11 = 9

$curTotal = 0;
$prevTotal = 0;

while (odbc_fetch_row($rs)) {
for ($col = $curStart; $col <= $curEnd; $col++) {
	if ($col == 1) $colName = 'SALES_OLDEST_PD1';
	elseif($col == 26) $colName = 'SALES_LAST_PD_26';
	elseif($col == 27) $colName = 'SALES_CURR_PD_27';
	else $colName = 'SALES_PD_' . $col;
	$curTotal += odbc_result($rs,$colName);
}

for ($col = $prevStart; $col <= $prevEnd; $col++) {
	if ($col == 1) $colName = 'SALES_OLDEST_PD1';
	elseif($col == 26) $colName = 'SALES_LAST_PD_26';
	elseif($col == 27) $colName = 'SALES_CURR_PD_27';
	else $colName = 'SALES_PD_' . $col;
	$prevTotal += odbc_result($rs,$colName);
}
}

 

Thank you. That is exactly was I was looking for.

 

I changed $prevEnd to equal 15.

$curEnd = 27;	// 27
$curStart = 27 - abs(11-date('m'));	// 27 - 6 = 21

$prevEnd = 15;// 15
$prevStart = $prevEnd - 11;	// 20 - 11 = 9

 

I have a question. Looking to the future... Like when the date becomes November of this year, will "abs(11-date('m'))" equal zero? October of this year would equal 13.... And December of this year would equal 1?

Oh, you're looking for Previous Year-To-Date (i.e. Nov 2010 thru May 2011, and Nov 2009 thru May 2010). To make it scale, set $prevEnd to $curEnd - 12. Also, it appears that I tested the only two months of the year that that formula works for (May and December). Try this approach to make the calculations independent of the date:

 

$thisMonth = intval(date('m'));
$curEnd = 27;	// Always the current period
$curStart = ($thisMonth <= 11 ? $curEnd - ($thisMonth + 1) : $curEnd - ($thisMonth - 11)); // November

$prevEnd = $curEnd - 12;// This month a year ago
$prevStart = $curStart - 12;	// Previous November 

 

You'll want to test that, but I think it is correct.

Thanks.  I will change it to your update and test it out.  So, far this code below is working great. I wound up changing $prevEnd to 15 since it will always be SALES_PD_15.

$curEnd = 27;	// 27
$curStart = 27 - abs(11-date('m'));	// 27 - 6 = 21

$prevEnd = 15;	// 15
$prevStart = $prevEnd - abs(11-date('m'));//15-6=9






$McurTotal = 0;
$MprevTotal = 0;

while (odbc_fetch_row($rs)) {
for ($col = $curStart; $col <= $curEnd; $col++) {
	if ($col == 1) $colName = 'SALES_OLDEST_PD1';
	elseif($col == 26) $colName = 'SALES_LAST_PD_26';
	elseif($col == 27) $colName = 'SALES_CURR_PD_27';
	else $colName = 'SALES_PD_' . $col;
	$McurTotal += odbc_result($rs,$colName);
}

for ($col = $prevStart; $col <= $prevEnd; $col++) {
	if ($col == 1) $colName = 'SALES_OLDEST_PD1';
	elseif($col == 26) $colName = 'SALES_LAST_PD_26';
	elseif($col == 27) $colName = 'SALES_CURR_PD_27';
	else $colName = 'SALES_PD_' . $col;
	$MprevTotal += odbc_result($rs,$colName);
}

$M27 += odbc_result($rs,"SALES_CURR_PD_27");
$M15 += odbc_result($rs,"SALES_PD_15");
}
/*
$MyearDiff= 100-($MprevTotal*100/$McurTotal);
$MmonthDiff= 100-($M15*100/$M27);
$MyearDiff = number_format($MyearDiff, 2, '.', ',');
     $MmonthDiff = number_format($MmonthDiff, 2, '.', ',');	
*/

$McurTotal = number_format($McurTotal, 2, '.', ',');
      $MprevTotal = number_format($MprevTotal, 2, '.', ',');	
$M27 = number_format($M27, 2, '.', ',');
      $M15 = number_format($M15, 2, '.', ',');	  

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.