Displaying the Date and Time of the Latest Activity on a Movable Type Blog

| 1 Comment | No TrackBacks |
I recently needed to show the date and time of the last activity on a blog: in other words, when was the last entry or comment posted?  For this to work, you need to know when the last entry was posted, when the last comment was posted and which happened last.  Here is the template code I came up with:
<mt:entries lastn="1">
<mt:entrydate format="%Y%m%d%H%M%S" setvar="lastentrydate">
<mt:setvarblock name="lastactivity{$lastentrydate}">
<p>Latest activity on <mt:entrydate format="%e %B at %l:%M%p"></p>
</mt:setvarblock>
</mt:entries>  

<mt:comments lastn="1" sort_order="desc">
<mt:commentdate format="%Y%m%d%H%M%S" setvar="lastcommentdate">
<mt:setvarblock name="lastactivity{$lastcommentdate}">
<p>Latest activity on <mt:commentdate format="%e %B at %l:%M%p"></p>
</mt:setvarblock>
</mt:comments>
                
<mt:loop name="lastactivity" sort_by="key numeric reverse">
<mt:if name="__counter__" lte="1"><mt:var name="__value__"></mt:if>
<mt:var name="__key__" setvar="key">
<mt:var name="lastactivity" key="$key" function="delete" setvar="devnull">
</mt:loop>

As you can see, first the date/time of the most recent entry is used as the key in a hash called 'lastactivity'.  This date/time is put in a format like YYYYMMDDHHMMSS, so in effect it is a really big number.  The value of the hash element is the same date, but in a nicer format with some HTML markup and text added.

We then do the same for the date/time of the most recent comment.

Finally, we loop over the hash, sorting it reverse numerically by key, so that the biggest number (= most recent date) ends up as the first element.  We only display the first element of the hash, ignoring the rest.  

Note that we also delete the elements in the hash again.  Strictly speaking this isn't needed, except if you use the above code snipped inside an <mt:blogs> loop to show the latest activity on multiple blogs.  If you omitted this line, previous comment and entry dates will 'stick' in the hash and the most recent one will show up time and again (until it gets replaced by a more recent comment/entry from some blog down in the loop).

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/1408

1 Comment

Hello Maarten,

I’ve been trying to display the 10 most recent comments on my blog in a sidebar. This sidebar is included as a widget in the entry archive template. What happens, instead of what I want, is that the 10 most recent comments are selected only for the entry in context…

Do you know how I can make sure the operates in the blog context instead of the entry context? I’m using Melody 1.0.

Thanks!

<mt:If tag="BlogCommentCount">
    <mt:Comments lastn="10" sort_order="descend">
    <mt:CommentsHeader>
    <h4>Latest Comments</h4>
        <ul>
        </mt:CommentsHeader>
            <li><strong><$mt:CommentAuthor$>:</strong> <$mt:CommentBody remove_html="1" words="10"$> <a href="<$mt:CommentLink>" title="full comment on: <mt:CommentEntry><$mt:EntryTitle$></mt:CommentEntry>">read more</a></li>
        <mt:CommentsFooter>
        </ul>
        </mt:CommentsFooter>
    </mt:Comments>
</mt:If>

Leave a comment