Jump to content

Dynamic Months


Xtremer360

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.