Weblog Entry Date Offset in ExpressionEngine

I recently had a client ask me to have their “Blog Archive” page display all of their previous weblog entries - except for entries posted in the last week.

That seems like a pretty reasonable request, right? Weblog entry display dates should be able to be sliced and diced however the user should want, with a minimum of work. Unfortunately, looking through EllisLab’s extensive weblog entry tag parameters documentation, I couldn’t find anything like a date offset function. The closest exp:weblog:entries paramaters I could find were the start_on and stop_before parameters. The problem is, the stop_before parameter would have to be dynamic: it would have to be consistently set to seven days before the current date.

I was able to solve that problem with a little PHP - read on for the code.

Here we go: this is the opening exp:weblog:entries tag I used to start the weblog entry display.

{exp:weblog:entries weblog="blog" orderby="date" pagination="bottom"
start_on="2009-04-26 00:00 " stop_before="<?php $datesubtract = time() - (60*60*24*7);
$datemin1 = date('Y-m-d H:i', $datesubtract); echo $datemin1; ?>" 
status="open"

This displays all weblog entries from the start date - April 26th, in this case - and seven days before the current date. To change the date range, change the “7” in the PHP to the number of days you want.

It’s that simple. Make sure to enable PHP for the template you put this in, and make sure that PHP for the template is set to be parsed on input, instead of the default output.

Let me know in the comments if you have any questions!


Posted on Jun 02, 2009 - 01:51 PM

Leave A Comment!!

Please enter the letters or word you see in the image below:

Name:

Email:

Location:

URL:

Remember this information for next time

E-mail me about follow-up comments