jakebur01 Posted May 19, 2011 Share Posted May 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/ Share on other sites More sharing options...
jakebur01 Posted May 19, 2011 Author Share Posted May 19, 2011 What about this? $start = date("m"); if($start<"11") { $backtoNovember= $start + 1; } elseif($start=="11") { $backtoNovember="0"; } else { $backtoNovember="1"; } Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1217769 Share on other sites More sharing options...
Maq Posted May 19, 2011 Share Posted May 19, 2011 Are these dates going to be entered manually or is it always *this* month? If always this month: echo abs(11-date('m')); Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1217771 Share on other sites More sharing options...
jakebur01 Posted May 19, 2011 Author Share Posted May 19, 2011 No, it will always stay the same. November is the beginning of fiscal year for a business. I'm just trying to count back to November. Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1217775 Share on other sites More sharing options...
jakebur01 Posted May 19, 2011 Author Share Posted May 19, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1217780 Share on other sites More sharing options...
jakebur01 Posted May 19, 2011 Author Share Posted May 19, 2011 Just to clarify. In the example of today's date. $MCYTD would equal the sum of $M21 - $M27. $MLYTD would equal the sum of $M9 - $M15. Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1217793 Share on other sites More sharing options...
jakebur01 Posted May 19, 2011 Author Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1217803 Share on other sites More sharing options...
DavidAM Posted May 19, 2011 Share Posted May 19, 2011 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1217810 Share on other sites More sharing options...
jakebur01 Posted May 20, 2011 Author Share Posted May 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1218010 Share on other sites More sharing options...
DavidAM Posted May 20, 2011 Share Posted May 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1218211 Share on other sites More sharing options...
jakebur01 Posted May 20, 2011 Author Share Posted May 20, 2011 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, '.', ','); Quote Link to comment https://forums.phpfreaks.com/topic/236901-count-how-many-months-it-is-back-to-november/#findComment-1218221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.