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