Jump to content

Archived

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

CanMan2004

php months

Recommended Posts

Hi all

I have a list of months on my websites events page, they look like

March 2006
April 2006
May 2006
July 2006
August 2006
September 2006
October 2006

the html code I use for each one is

<a href="?month=03&year=06">March 2006</a>
<a href="?month=04&year=06">April 2006</a>
<a href="?month=05&year=06">May 2006</a>

and so on.

Is it possible in php, for, that the start of a new month, for that month to be automatically added to the bottom of the list on the page and insert the relevant href code?

I had a hunt around, but couldnt find anything that worked.

Any help or guidence, would be great

Thanks in advance

Ed

Share this post


Link to post
Share on other sites
It's possible, and simple.  How do you want them listed?  For this month and back six months, from six months in the past to six months in the future, etc.  It's a fairly trivial code exercise, but we might as well get it going in the right direction from the start.

Share this post


Link to post
Share on other sites
[code]list($year,$month) = explode("-", date("Y-m")); // get todays year/month values[/code]

That will give you a 2-digit month number and a four-digit year number for today. You can construct your html/php link using $month and $year. Does that point you in the right direction?

Oh, I assume that next month you want links for October and November?  That's why I asked how far back (or forward) you wanted these links for.

Share this post


Link to post
Share on other sites
let's assume you want to always start with March of '06 and go to the current month. all you'd have to do iw this:
[code]
<?php
$ts = mktime(0,0,0,3,1,2006);
while ($ts != mktime(0,0,0,date('m') + 1, 1, date('Y'))) {
  list($m, $y) = explode('-', date('m-y', $ts));
  list($month, $year) = explode('-', date('F-Y', $ts));
  echo "<a href=\"?year=$y&amp;month=$m\">$month $year</a><br />\n";
  $ts = mktime(0,0,0,$m+1,1,$year);
}
?>
[/code]

Share this post


Link to post
Share on other sites
Hi again

Further to this, is it possible to perform a query on a table called "events" which checks to see how many records are stored for each month listed? I store my dates in the sql database as

DATE 0000-00-00

Any help would be appricated

Thanks

Ed

Share this post


Link to post
Share on other sites
I guess that I need to use

[code]$countsql = mysql_result(mysql_query("SELECT COUNT(*) AS NUM FROM events WHERE date =", $connection),0);[/code]

and put it within the query supplied in this thread, but how do I get it to query the month 10 for example if a date is stored as 0000-00-00, how do you tell the query to just check the month part of the date field.

Share this post


Link to post
Share on other sites
well, here's the basic idea:
[code]
<?php
$sql = mysql_query("SELECT MONTH(dateCol) AS month, YEAR(dateCol) AS year, COUNT(*) AS count FROM tableName GROUP BY month, year ORDER BY year ASC, month ASC");
$res = array();
while ($row = mysql_fetch_array($sql)) $res[] = $row;

// now, you have a list of counts of all months from the oldest to the newest
// just apply the code above to these records:
$ts = mktime(0,0,0,3,1,2006);
while ($ts != mktime(0,0,0,date('m') + 1, 1, date('Y'))) {
  list($m, $y) = explode('-', date('m-y', $ts));
  list($month, $year) = explode('-', date('F-Y', $ts));
  echo "<a href=\"?year=$y&amp;month=$m\">$month $year</a>\n";
  $count = 0; // default to zero
  foreach ($res as $row) {
    if ($row['month'] == $m && $row['year'] == $year) $count = $row['count'];
  }
  echo "$count entries<br />\n";
  $ts = mktime(0,0,0,$m+1,1,$year);
}
?>
[/code]

make sense?

Share this post


Link to post
Share on other sites
[quote author=CanMan2004 link=topic=110072.msg444282#msg444282 date=1159632482]
I guess that I need to use

[code]$countsql = mysql_result(mysql_query("SELECT COUNT(*) AS NUM FROM events WHERE date =", $connection),0);[/code]

and put it within the query supplied by you good guys, but how do I get it to query the month 10 for example if a date is stored as 0000-00-00, how do you tell the query to just check the month part of the date field.
[/quote]

basically, what i've done is written it to do what you're after in one query. if you want to query for each record, that's up to you, but you'd need to apply the MONTH() and YEAR() SQL functions like i've shown into your queries.

Share this post


Link to post
Share on other sites

×

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.