Xtremer360 Posted September 8, 2011 Share Posted September 8, 2011 I'm trying to figure out for the months I can get it to display the last 5 months including the current month. The reason for this is I"m going to get the total number of hits for my website for each month as well as the number of successful logins for my CMS script. Any thoughts? <table class="visualize_dashboard"> <caption> Dashboard Chart Example </caption> <thead> <tr> <td></td> <th scope="col">March</th> <th scope="col">April</th> <th scope="col">May</th> <th scope="col">June</th> <th scope="col">July</th> </tr> </thead> <tbody> <tr> <th scope="row">Visits</th> <td>175</td> <td>145</td> <td>212</td> <td>175</td> <td>182</td> </tr> <tr> <th scope="row">Logins</th> <td>94</td> <td>53</td> <td>124</td> <td>92</td> <td>105</td> </tr> </tbody> </table> Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/ Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Have you at least attempted this? All I see is HTML. Show me some code and I'll help further. Do you need the year as well? Do you need the number of days in the given month? Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267101 Share on other sites More sharing options...
Xtremer360 Posted September 8, 2011 Author Share Posted September 8, 2011 I know how to do the date function but I"m not sure how to have it display just the last 5 monts. Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267109 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Have you at least attempted this? ... Show me some code and I'll help further. Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267111 Share on other sites More sharing options...
Xtremer360 Posted September 8, 2011 Author Share Posted September 8, 2011 Is this right? echo date("M") ; echo date("M", strtotime("-1 month") ) ; echo date("M", strtotime("-2 month") ) ; echo date("M", strtotime("-3 month") ) ; echo date("M", strtotime("-4 month") ) ; Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267112 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 That will work, but using the date function is slow, so if we can avoid using it multiple times it's better. Here's how I would approach this <?php $month = date('M'); for( $i = 0; $i < 5; $i++ ) { echo $month.' '; $month = getPrevMonth($month); } function getPrevMonth( $month ) { // Build an array of months, 0 = Jan, 11 = Dec $months = array('Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'); // Grab the key of the current month, if it can't be found, return FALSE if( ($n = array_search($month,$months)) === FALSE ) return FALSE; // If month is Jan, return Dec if( $n == 0 ) return $months[11]; // Otherwise, return key-1 else return $months[$n-1]; } ?> Then again, there's nothing wrong with your solution. Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267122 Share on other sites More sharing options...
Pikachu2000 Posted September 8, 2011 Share Posted September 8, 2011 EDIT: Pretty much what xyph said . . . ^^^ Something like this would work, so you don't have to keep calling date(): <?php $months = array( 1 => 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER' ); $cur_month = (int) date('m'); echo '<select name="month">'; for( $i = $cur_month; $i > ($cur_month - 5) ; $i-- ) { echo "<option value=\"$i\">$months[$i]</option>\n"; } echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267124 Share on other sites More sharing options...
Xtremer360 Posted September 8, 2011 Author Share Posted September 8, 2011 That will work, but using the date function is slow, so if we can avoid using it multiple times it's better. Here's how I would approach this <?php $month = date('M'); for( $i = 0; $i < 5; $i++ ) { echo $month.' '; $month = getPrevMonth($month); } function getPrevMonth( $month ) { // Build an array of months, 0 = Jan, 11 = Dec $months = array('Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'); // Grab the key of the current month, if it can't be found, return FALSE if( ($n = array_search($month,$months)) === FALSE ) return FALSE; // If month is Jan, return Dec if( $n == 0 ) return $months[11]; // Otherwise, return key-1 else return $months[$n-1]; } ?> Then again, there's nothing wrong with your solution. So your saying if I have my dashboard.php file which handles all the php related tasks I can do this: <?php $month = date('M'); for( $i = 0; $i < 5; $i++ ) { echo $month.' '; $month = getPrevMonth($month); } function getPrevMonth( $month ) { // Build an array of months, 0 = Jan, 11 = Dec $months = array('Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'); // Grab the key of the current month, if it can't be found, return FALSE if( ($n = array_search($month,$months)) === FALSE ) return FALSE; // If month is Jan, return Dec if( $n == 0 ) return $months[11]; // Otherwise, return key-1 else return $months[$n-1]; } ?> And then on my normal dashboard.php page I can do this: <thead> <tr> <td></td> <th scope="col"><?php echo $month ?></th> </tr> </thead> Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267131 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Pikachu - Don't forget to include a check for months < 1. For example, if the current month was February you would get undefined offset errors @CoolAsCarlito - if that's what you want your output to be. I don't provide code you can just plug in and work - that would defeat the whole 'helping' aspect of the forum and turn it into a 'free programming' forum. Modify the code I've provided to make it work for you. If there is any part you're unsure about, ask. I will NOT provide you a copy+paste solution though. Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267133 Share on other sites More sharing options...
Pikachu2000 Posted September 8, 2011 Share Posted September 8, 2011 Quite true. I pretty much threw that together as an example for him. I should probably finish it up and save it somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267141 Share on other sites More sharing options...
Xtremer360 Posted September 8, 2011 Author Share Posted September 8, 2011 Is this where I use google analytics to get the numbers of hits to my site for each month? Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267152 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Quite true. I pretty much threw that together as an example for him. I should probably finish it up and save it somewhere. You could probably re-write that in the same time it would take you to find an archived version Is this where I use google analytics to get the numbers of hits to my site for each month? Que? Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267155 Share on other sites More sharing options...
Xtremer360 Posted September 8, 2011 Author Share Posted September 8, 2011 Is this where I use google analytics to get the numbers of hits to my site for each month? Que? Huh? Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267156 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 In English - What? You never mentioned anything about Google Analytics previously, nor are we going to support a 3rd party script or API in a PHP help forum. Do you realize how difficult it is for us to test and create code for an external API we may or may not have access to? Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267159 Share on other sites More sharing options...
Xtremer360 Posted September 8, 2011 Author Share Posted September 8, 2011 I didn't know much about it and just asking a general inquiry if that's the correct method to achieve it. Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267161 Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 I don't know. The manual for the Google Analytics API, or a forum that supports it might be a better place to look. Quote Link to comment https://forums.phpfreaks.com/topic/246740-dynamic-months/#findComment-1267165 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.