Help - Search - Members - Calendar
Full Version: How To Create An Automatic Events Calendar
Movable Type Community Forum > Using Movable Type > Entries and Archives
etokle
Hey all. During a recent job the client had need of a calendar of events that would:

1. Display the upcoming week's events including the body of the post.
2. Automatically remove events which had passed
3. Display upcoming events in a simple title list.
4. Move events from the "upcoming" view to the expanded view once they were within the next week

After much searching and hours of hackery I achieved each of my requirements! The results can be viewed at indierecordshop.com by clicking "Live and Local".

This list is entirely dynamic and is built from normal pre-dated posts. I figured that at least one other Movable-Typer out there might have need for such a list so I thought I'd post the code here for anyone who needs it. Oh yeah, you'll need to be using PHP.

First, you're going to create a new template and copy/paste the following code:


CODE
<?php

function DateAdd($interval, $number, $date) {

   $date_time_array = getdate($date);
   $hours = $date_time_array['hours'];
   $minutes = $date_time_array['minutes'];
   $seconds = $date_time_array['seconds'];
   $month = $date_time_array['mon'];
   $day = $date_time_array['mday'];
   $year = $date_time_array['year'];

   switch ($interval) {
   
       case 'yyyy':
           $year+=$number;
           break;
       case 'q':
           $year+=($number*3);
           break;
       case 'm':
           $month+=$number;
           break;
       case 'y':
       case 'd':
       case 'w':
           $day+=$number;
           break;
       case 'ww':
           $day+=($number*7);
           break;
       case 'h':
           $hours+=$number;
           break;
       case 'n':
           $minutes+=$number;
           break;
       case 's':
           $seconds+=$number;
           break;            
   }
      $timestamp= mktime($hours,$minutes,$seconds,$month,$day,$year);
   return $timestamp;
}

?>


Save this template with both template name and output file set to dateadd.inc.

This file is simply a function that will be called by the entries template (index or whatever). The function takes 3 arguments: the interval (day, week, month), the number of days (or whatever interval) that should be included, and the current date. These parameters will be defined by the entry template. The date returned by the function is a date x-amount of y-intervals from z-date. So in the example of:

CODE
$temptime = time();
    $temptime = DateAdd('d',7,$temptime);


DateAdd will return the date 7 days in the future (in this case "$temptime" first picks up the current time and is then reset to the date returned by DateAdd). Note that these variables are defined in the index template.

The beauty of this function is you can write it once and forget it. Any time you need to call it you will just use a php include on the page you are using it on.


Ok, so now the index template. This is where the entries are moved around and displayed based on their date. Here's the relevant section of the template as I used it on indierecordshop.com :


CODE
<?php
include "<$MTBlogURL$>dateadd.inc";
$temptime = time();
$temptime = DateAdd('d',7,$temptime);
$nextweek=date('Ymd',$temptime);
$now=date("Ymd");
?>

<h2>This Week's Events</h2>
   <MTEntries sort_order="ascend">
   <?  $entry=date("<$MTEntryDate format="%Y%m%d"$>");
    if ($now<=$entry) {
       if ($entry<=$nextweek){
    ?>      
    <MTDateHeader>
         <h4><$MTEntryDate format="%A, %x"$></h4>
         </MTDateHeader>

         <b id="a<$MTEntryID pad="1"$>" class="thisweek"><$MTEntryTitle$></b><br>
    <$MTEntryBody$>
 <MTEntryIfExtended>
       <p class="extended"><a href="<$MTEntryPermalink$>">Continue reading "<$MTEntryTitle$>"</a></p>
       </MTEntryIfExtended> <br/>

    <MTDateFooter>
   <p class="posted"></p>
    </MTDateFooter>
 <? }
   } ?>
   </MTEntries>

<h2>Upcoming Events</h2>
   <MTEntries sort_order="ascend">
   <?  $entry=date("<$MTEntryDate format="%Y%m%d"$>");
    if ($now<=$entry) {
 if ($entry>$nextweek) {
    ?>
     <$MTEntryDate format="%A, %B %d"$> -- <a href="<$MTEntryPermalink$>"><i id="a<$MTEntryID pad="1"$>"><$MTEntryTitle$></i></a><br>
 <? }
   } ?>
   </MTEntries>


First we set up the variables:
1. $temptime is the date returned from the DateAdd function
2. $nextweek is $temptime formatted Ymd
3. $now is, well, now.

The two sections are This Week and Next Week's Events. In each set of entries each entry has its date formatted; this formatted date is then compared to $now. If the entry date is larger than now it is not displayed. In the next loop, the entry is compared to $nextweek. In the first set of entries, if the entry date is less than or equal to next week's date, it is displayed. The only difference in the second entry set is the entry date must be larger than next week's date.

That's it. You now have an events calendar (or whatever kind of entries you want to use) that will automatically move items based upon their date. If you want only the next 3 days to be included in the upcoming list, just change the second argument of DateAdd to 3. You can do a lot more with the DateAdd function, this is merely one example.

Oh yeah, remember to set the "authored on date" in each entry to the date you want it to be associated with. Say, if an event is December 1 pre-date the authored on date as December 1st.

Keep in mind that I am an absolute beginner with both Movable Type and PHP, so I'm sure more advanced users will find ways to make this code more efficient. I welcome any improvements or comments. Good luck with building your Movable Type events calendar!
-=e=-

etokle.com
fuzzygerdes
Thanks! This is exactly what I needed.
serankko
Hi,

I used this code and works really good but I need some help...

I just want to post on my event calendar some categories and not all of them, How I can do it huh.gif What I need to modify on this code huh.gif

Thanks for your help....
sarah
Have you tried adding the category attribute to the <MTEntries> tags used in the code above?

Currently it is (in two instances):

CODE
<MTEntries sort_order="ascend">


To specify entries from a particualr category, you might try the following (where Your Category is the name of the category you want to use):

CODE
<MTEntries category="Your Category" sort_order="ascend">


This would be the way to specify the categories included in a regular MTEntries list, so it seems like it would be worth a shot here. smile.gif
kelake
I get this error:

Warning: main(): URL file-access is disabled in the server configuration in /usr/www/users/xxxx/xxx/events/index.php on line 86

Warning: main(http://www.xxxxxx.com/events/dateadd.inc): failed to open stream: no suitable wrapper could be found in /usr/www/users/xxxxx/xxx/events/index.php on line 86

Warning: main(): Failed opening 'http://www.xxxxxx.com/events/dateadd.inc' for inclusion (include_path='.:/usr/local/lib/php') in /usr/www/users/xxxxx/xxx/events/index.php on line 86

Fatal error: Call to undefined function: dateadd() in /usr/www/users/xxxx/xxx/events/index.php on line 88
sarah
This is a PHP error, likely because your server has disallowed including files referenced with an URL - so you'll want to try referencing the file to be included with the server path instead.

To do this, try replacing:

CODE
include "<$MTBlogURL$>dateadd.inc";


With:

CODE
include "<$MTBlogSitePath$>/dateadd.inc";


The MTBlogSitePath tag will output the server path to the weblog directory, rather than the URL (which MTBlogURL does).
kelake
QUOTE (sarah @ May 18 2006, 07:45 AM)
This is a PHP error, likely because your server has disallowed including files referenced with an URL - so you'll want to try referencing the file to be included with the server path instead.

To do this, try replacing:

CODE
include "<$MTBlogURL$>dateadd.inc";


With:

CODE
include "<$MTBlogSitePath$>/dateadd.inc";


The MTBlogSitePath tag will output the server path to the weblog directory, rather than the URL (which MTBlogURL does).
*


That did it! Thank you very much Sarah.
sugarbeth
I've got this going on my site, and it seems to be working fine.

I was just wondering if there was a simple way to limit how far in advance the "Upcoming Events" displays posts? Like just 3 months ahead or something? So if I wanted to fill out Events for the year ahead of time, it wouldn't list ALL of the future ones.

TIA!
schwarza76
Hi, great stuff you added here , its very usefull for me.
I use it for a javascript countdown to a specific date in future but there is one issue, i can't get working.

For now, it shows all events sheduled in 5 days. If there are two events in the next 5 days it shows my countdown twice.
Is there a way to limit the output to only one entry? lastn doen't work for that.

Any ideas?

You can see the countdown on the top of the sidebar. LINK

And here, the code I use:


CODE
<?php
include "<MTStaticWebPath>rfactorytermine/dateadd.inc";
$temptime = time();
$temptime = DateAdd('d',5,$temptime);
$nextweek=date('Ymd',$temptime);
$now=date("Ymd");
?>

<div id="counter">
   <MTEntries sort_order="ascend">
   <?  $entry=date("<$MTEntryDate format="%Y%m%d"$>");
if ($now<=$entry) {
       if ($entry<=$nextweek){
?>      

<div>
&lt;script language="JavaScript">
                                TargetDate = "<MTEntryDate format="%m/%d/%Y %X">";
                                BackColor = "transparent";
                                ForeColor = "#EFEFEF";
                                CountActive = true;
                                CountStepper = -1;
                                LeadingZero = true;
                                DisplayFormat = "<span>%%D%%</span><span>%%H%%</span><span>%%M%%</span><span>%%S%%</span>";
                                 </script>
         &lt;script language="JavaScript" src="<MTBlogURL>countdown.js"></script>

</div>
<p style="margin-top: 2px; background: #313131; letter-spacing: 3px; font-weight: bold; text-align: center""><MTExtraFields><MTIfExtraFields field="track"><$MTExtraFieldValue field="track"$></MTIfExtraFields></MTExtraFields></p>
<img src="<MTStaticWebPath>rfactory/images/countdown/<MTExtraFields><MTIfExtraFields field="series"><$MTExtraFieldValue field="series"$></MTIfExtraFields></MTExtraFields>.jpg" width="294" height="69" alt="keine Vorschau verfügbar" title="" />
<p style="margin-top: 4px;">&raquo; <MTEntryDate format="%a. %d. %m.">  &raquo;  <MTEntryTitle><span style="color: #42B9C9"> / </span><a href="<MTEntryPermalink>">Info</a></p>

<? }
   } ?>
   </MTEntries>
</div>
</div>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.