At long last, we're moving to forums powered by, well, Movable Type itself. You'll want to bookmark http://forums.movabletype.org/ for future reference, and in the meantime you can view these old forums as a read-only archive of past posts. Thanks for being part of the community!
![]() ![]() |
Jul 22 2005, 01:41 PM
Post
#1
|
|
|
Group: Members Posts: 2 Joined: 16-May 05 Member No.: 31,329 |
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 |
|
|
|
Aug 11 2005, 07:44 PM
Post
#2
|
|
|
Group: Members Posts: 9 Joined: 4-January 04 From: Chicago Member No.: 19,369 |
Thanks! This is exactly what I needed.
|
|
|
|
May 10 2006, 06:59 PM
Post
#3
|
|
|
Group: Members Posts: 1 Joined: 10-May 06 Member No.: 35,451 |
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 Thanks for your help.... |
|
|
|
May 10 2006, 07:35 PM
Post
#4
|
|
|
Technical Services Group: Six Apart Moderators Posts: 1,088 Joined: 30-October 05 Member No.: 33,516 |
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. -------------------- |
|
|
|
May 17 2006, 10:53 PM
Post
#5
|
|
|
Group: Members Posts: 52 Joined: 5-May 02 From: Hsinchu, Taiwan Member No.: 1,544 |
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 This post has been edited by sarah: May 17 2006, 11:42 PM -------------------- |
|
|
|
May 17 2006, 11:45 PM
Post
#6
|
|
|
Technical Services Group: Six Apart Moderators Posts: 1,088 Joined: 30-October 05 Member No.: 33,516 |
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). This post has been edited by sarah: May 17 2006, 11:45 PM -------------------- |
|
|
|
May 18 2006, 12:47 AM
Post
#7
|
|
|
Group: Members Posts: 52 Joined: 5-May 02 From: Hsinchu, Taiwan Member No.: 1,544 |
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. -------------------- |
|
|
|
Aug 25 2006, 04:31 PM
Post
#8
|
|
|
Group: Members Posts: 47 Joined: 27-October 03 Member No.: 17,081 |
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! |
|
|
|
Sep 3 2007, 05:53 AM
Post
#9
|
|
|
Group: Members Posts: 17 Joined: 28-December 04 Member No.: 28,855 |
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> <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> <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;">» <MTEntryDate format="%a. %d. %m."> » <MTEntryTitle><span style="color: #42B9C9"> / </span><a href="<MTEntryPermalink>">Info</a></p> <? } } ?> </MTEntries> </div> </div> This post has been edited by schwarza76: Sep 3 2007, 05:55 AM |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 11.24.09 - 09:54 PM |