Jump to content

Simple Month List


project18726

Recommended Posts

Hi everyone,

 

First, let me say I'm sure my code and queries can be optimized so please don't rip me apart on that just yet. I want to get the basic functionality working before I work on that. But here's my problem: I can't get a list of months between an account creation date and now to display. It's driving me f'ing nuts. It's probably something very simple I'm overlooking. A fresh pair of eyes maybe???

 

My first (and working) function goes like this:

 

//------ List Months Since Account Creation ------

function list_months_since_creation(){

$getmonth = mysql_query("SELECT DATE_FORMAT(date_created, '%m') FROM contacts WHERE contact_id=1") or die(mysql_error());
$getyear = mysql_query("SELECT DATE_FORMAT(date_created, '%Y') FROM contacts WHERE contact_id=1") or die(mysql_error());

$outputmonth = mysql_fetch_array($getmonth);
$outputyear = mysql_fetch_array($getyear);

$monthcreated = $outputmonth[0]+1;
$yearcreated = $outputyear[0];

$currentdate = date('F Y');
$currenttimestamp = strtotime($currentdate);

echo "<span class='bold'>".$outputyear[0]."</span><br>";

while ($createddatetimestamp < $currenttimestamp) {
$createddatetimestamp = mktime(0,0,0,$monthcreated,0,$yearcreated);
if(date('F', $createddatetimestamp) == "January"){
echo "<br><span class='bold'>".date('Y', $createddatetimestamp)."</span>";
};
echo "<div class='month_row'><div class='display_month'>  ".date('F', $createddatetimestamp)."</div></div>";
$monthcreated++;
};

};

//----------------------------------------------

 

And that works excellent. It does exactly what I was hoping it would do -- list the months in order from the account creating date until now. Then I realized that it should probably list the most recent months at the top and older months at the bottom. And so I tried rewriting it a little bit and came up with this so far:

 

//------ List Months Since Account Creation In Reverse Order------

function list_months_since_creation_reverse(){

$getmonth = mysql_query("SELECT DATE_FORMAT(date_created, '%m') FROM contacts WHERE contact_id=1") or die(mysql_error());
$getyear = mysql_query("SELECT DATE_FORMAT(date_created, '%Y') FROM contacts WHERE contact_id=1") or die(mysql_error());

$outputmonth = mysql_fetch_array($getmonth);
$outputyear = mysql_fetch_array($getyear);

$monthcreated = $outputmonth[0]+1;
$yearcreated = $outputyear[0];

// $currenttimestamp = mktime(0,0,0,date("m"),0,date("Y"));
$createddatetimestamp = mktime(0,0,0,$monthcreated,0,$yearcreated);
$subtract = 0;

while ($currentsubtractedtimestamp > $createddatetimestamp) {
  $currentsubtractedtimestamp = mktime(0,0,0,(date("n")-$subtract),0,date("Y"));
  echo "<div class='month_row'><div class='display_month'>  ".date('F', $currentsubtractedtimestamp)." ".date('Y', $currentsubtractedtimestamp)."</div></div>";
  $subtract++;
  echo $createddatetimestamp."<br>";
};

};

//----------------------------------------------

 

When I try to echo the reverse function it doesn't display a damn thing. I don't know, I assume it's calculating that the current timestamp is already less than the created timestamp.

 

I would greatly appreciate any help on this.

Link to comment
https://forums.phpfreaks.com/topic/125211-simple-month-list/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.