Jump to content

event calendar by month instead of day


samoht

Recommended Posts

hello all,

 

I have an event calendar that is quite clunky for a business that often only has 1 or 2 events in a month and would like to view all upcoming events (or at least with in the next 6mo. to a year) so I would like to change from displaying a traditional calendar by the month showing every day - to a two column list of the next 6 to 12 months that becomes a link if there are any events within the month ( and probably display the number of events next to the month...e.g. August (3) etc.)

 

Any thought on how I would construct the php loops for the months?

 

Thanks for any help

 

Andy

Link to comment
Share on other sites

Use modulos (%)

 

<?php
$currentMonth = 4;
$count = 12;
$display = "";
while (true) {
    if (($counter % 2) == 0) {
         $display .= "<td>" . $currentMonth . "</td></tr>";
    }else {
         $display .= "<tr><td>" . $currentMonth . "</td>";
    }

    if ($currentMonth == 12) {
        $currentMonth = 0;  
    } 

    if ($counter == 12) {
        break; // kill the loop
    }

    $currentMonth++;
    $counter++;
}

echo "<table>" . $display . "</table>";
?>

 

Something like that?

Link to comment
Share on other sites

Thanks for the start,

 

quick question - If I wanted to display the months as 07, instead of 7 etc how would I change the code?

 

+ is there an easy way to retrieve the month name inside the loop?

 

Thanks

 

Put the months into an array.

 

<?php
$months = array(1=> array("num" => "01", "name" => "January"), 2 => array("num" => "02", "name" => "February")); // ..etc

$currentMonth = 4;
$count = 12;
$display = "";
while (true) {
    if (($counter % 2) == 0) {
         $display .= "<td>" . $months[$currentMonth]['name'] . "</td></tr>";
    }else {
         $display .= "<tr><td>" . $currentMonth[$currentMonth]['name'] . "</td>";
    }

    if ($currentMonth == 12) {
        $currentMonth = 0;  
    } 

    if ($counter == 12) {
        break; // kill the loop
    }

    $currentMonth++;
    $counter++;
}

echo "<table>" . $display . "</table>";
?>

 

Change 'name' to 'num' to get the number.

Link to comment
Share on other sites

<?php
$months = array(1=> array("num" => "01", "name" => "January"), 2 => array("num" => "02", "name" => "February")); // ..etc

$currentMonth = 4;
$count = 12;
$display = "";
while (true) {
    if (($counter % 2) == 0) {
         $display .= "<td>" . $months[$currentMonth]['name'] . "</td></tr>";
    }else {
         $display .= "<tr><td>" . $months[$currentMonth]['name'] . "</td>"; // error was here
    }

    if ($currentMonth == 12) {
        $currentMonth = 0;  
    } 

    if ($counter == 12) {
        break; // kill the loop
    }

    $currentMonth++;
    $counter++;
}

echo "<table>" . $display . "</table>";
?>

 

my bad.

Link to comment
Share on other sites

You can get the next 12 months as numbers and text without using arrays, by using the date() and strototime() functions. Might save some typing :P :

 

<?php
$curr_month= 7;
for($x=0;$x<12;$x++){

$month_num =($x+$curr_month > 12) ? $x+$curr_month -12 : $x+$curr_month;
echo date('m',strtotime($month_num.'/01'));
echo ' - '.date('F',strtotime($month_num.'/01')).'<br />';

}
?>

Produces:

07 - July
08 - August
09 - September
10 - October
11 - November
12 - December
01 - January
02 - February
03 - March
04 - April
05 - May
06 - June

Link to comment
Share on other sites

Thanks for the help!

 

The less code the better!

Anyway, one last element I need is the total number of events to display with the month

 

I have a query of two tables:

$query_rsEvents = "SELECT events.EventId, events.EventLocationId, events.Name, events.ShortDescr, events.Descr, events.ImageURL, events.PDF, eventdates.EventDate, eventdates.Note, eventdates.TimeStart, eventdates.TimeEnd FROM events, eventdates WHERE eventdates.EventId = events.EventId AND events.FlagStatus = 'A' ORDER BY eventdates.EventDate, eventdates.TimeStart, events.Name";
$rsEvents = mysql_query($query_rsEvents, $connection1) or die(mysql_error());
$row_rsEvents = mysql_fetch_assoc($rsEvents);
$totalRows_rsEvents = mysql_num_rows($rsEvents);

obviously I have more fields here than I need for this question.

 

Anyway, I want to total the number of events for each month, but I am not sure how to write the loop to work with the loop to give me the months?

 

I do want to display all 12 months in my list - but add the totalEvents counter to those months that have events.

 

Does this make sense.

Link to comment
Share on other sites

Why doesn't this work??

 

<?php

#This loop gets the next 6 months from current date
$curr_month= $CurMonth;
for($x=0;$x<6;$x++){

$month_num =($x+$curr_month > 12) ? $x+$curr_month -12 : $x+$curr_month;
#get the total number of events for each month
$i=0;
do{$i++;}
while ($row_rsEvents['EventDate'] = $curr_month);
echo date('m',strtotime($month_num.'/01'));
echo ' - '.date('F',strtotime($month_num.'/01'))." ( ". $i ." )<br />";

}
?>

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.