CanMan2004 Posted September 30, 2006 Share Posted September 30, 2006 Hi allI have a list of months on my websites events page, they look likeMarch 2006April 2006May 2006July 2006August 2006September 2006October 2006the 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 greatThanks in advanceEd Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/ Share on other sites More sharing options...
AndyB Posted September 30, 2006 Share Posted September 30, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101368 Share on other sites More sharing options...
CanMan2004 Posted September 30, 2006 Author Share Posted September 30, 2006 HiThanks for the reply, I just wanted to start it from the october ideally Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101371 Share on other sites More sharing options...
AndyB Posted September 30, 2006 Share Posted September 30, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101373 Share on other sites More sharing options...
obsidian Posted September 30, 2006 Share Posted September 30, 2006 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&month=$m\">$month $year</a><br />\n"; $ts = mktime(0,0,0,$m+1,1,$year);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101387 Share on other sites More sharing options...
CanMan2004 Posted September 30, 2006 Author Share Posted September 30, 2006 thank you, again, always saving the day Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101411 Share on other sites More sharing options...
CanMan2004 Posted September 30, 2006 Author Share Posted September 30, 2006 Hi againFurther 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 asDATE 0000-00-00Any help would be appricatedThanksEd Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101416 Share on other sites More sharing options...
CanMan2004 Posted September 30, 2006 Author Share Posted September 30, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101425 Share on other sites More sharing options...
obsidian Posted September 30, 2006 Share Posted September 30, 2006 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&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? Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101427 Share on other sites More sharing options...
obsidian Posted September 30, 2006 Share Posted September 30, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101428 Share on other sites More sharing options...
CanMan2004 Posted September 30, 2006 Author Share Posted September 30, 2006 Makes great sense, thank you Quote Link to comment https://forums.phpfreaks.com/topic/22590-php-months/#findComment-101430 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.