Jump to content


Photo

php months


  • Please log in to reply
10 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 30 September 2006 - 01:30 PM

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

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 September 2006 - 02:00 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#3 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 30 September 2006 - 02:05 PM

Hi

Thanks for the reply, I just wanted to start it from the october ideally

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 September 2006 - 02:12 PM

list($year,$month) = explode("-", date("Y-m")); // get todays year/month values

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.
Legend has it that reading the manual never killed anyone.
My site

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 September 2006 - 03:07 PM

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:
<?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);
}
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 30 September 2006 - 03:46 PM

thank you, again, always saving the day

#7 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 30 September 2006 - 03:52 PM

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

#8 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 30 September 2006 - 04:08 PM

I guess that I need to use

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

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.

#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 September 2006 - 04:08 PM

well, here's the basic idea:
<?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);
}
?>

make sense?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#10 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 September 2006 - 04:09 PM

I guess that I need to use

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

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.


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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#11 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 30 September 2006 - 04:11 PM

Makes great sense, thank you




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users