2

I want to embed a template if the entry_date of an entry is less than a certain age, and display something else otherwise. Is there a good way to do this natively? With addons? I tried the timespan plugin, but the documentation seems to halfway be for a previous version of the app that was named differently, and none of it seems to actually work.

In my specific case I want to embed the template if the entry is less than 3 years old, but otherwise display a message that we don't provide this information for old entries.

I think the most common useful case for this is a contact form (or comment field though there are addons for expiring comments) for each entry in a channel and you start to get hammered with spam on old entries.

4

Here are two options you could try. Vim Interactive has a date plug-in, DT Plugin, that lets you set the year offset within the plug-in tag itself. Solspace also has a date plug-in called Date Field Filter. To set the offset you require would mean you need to turn on PHP in your templates. I'd recommend trying Vim's plug-in first and see of this solves your problem before going down the Solspace route. Both plug-ins are free so there's no reason not to give them a shot.

update from further expirimentation:

embed tag:{embed="grp/tmp" ent_date="{exp:dt set="{entry_date format="%m/%d/%y"}" ee_format=“true" format="%y%m%d"}" 3_years="{exp:dt year="-3" ee_format=“true" format="%y%m%d"}"}

to add variables to the embedded template, and then:

{if {embed:ent_date} < {embed:3_years}}Old Entry Message{if:else}New Entry Content{/if}
4
  • Thanks Stuart, if I need to turn on PHP I can do the whole thing myself, I'm hoping to find someway to avoid that. – UltraBob Feb 18 '13 at 5:28
  • Just looked into this further, DT Plugin was neat in that I could make the following code: If {exp:dt set="{entry_date format="%m/%d/%y"}" ee_format=“true" format="%y%m%d"} is less than {exp:dt year="-3" ee_format=“true" format="%y%m%d"} this program is older than 3 years old Which explains what logically I'm trying to do, but I haven't managed to get the conditional part figured out. I guess maybe I can send the two numbers to the embed and do the conditional there, I may give that a try. The second one doesn't do what I want because I want to filter certain content, not the whole entry. – UltraBob Feb 18 '13 at 5:44
  • Stuart, I took your answer a little bit further and got it to work. Can you add this to your answer and then I'll accept it?: – UltraBob Feb 18 '13 at 8:31
  • embed tag:{embed="grp/tmp" ent_date="{exp:dt set="{entry_date format="%m/%d/%y"}" ee_format=“true" format="%y%m%d"}" 3_years="{exp:dt year="-3" ee_format=“true" format="%y%m%d"}"} to add variables to the embedded template, and then: {if {embed:ent_date} < {embed:3_years}}Old Entry Message{if:else}New Entry Content{/if} – UltraBob Feb 18 '13 at 9:20
1

you could check if the current date minus the date of the entry exceeds 3 years:

{if '{current_time format="%U"}' - '{entry_date format="%U"}' >= 31536000}
    // entry is older than 3 years
{/if} 

note that this isn't a 100% correct, as the '31536000' is the amount of seconds in a normal year, so this neglects leap-years..

2
  • 1
    Hi janvi I don't think you can do that. My testing has not shown mathematical operations to work in conditionals, just comparison operators. – UltraBob Feb 18 '13 at 8:30
  • While this isn't my exact problem, you saved me from further troubleshooting setting and testing Stash variables when what I really wanted was {if '{entry_date format="%Y%m%d"}' != '{edit_date format="%Y%m%d"}'} – Charlie Gorichanaz Jan 4 '14 at 6:34
0

Having had a quick look at the Timespan source code I think you'd want to try something like this if you haven't already:

{exp:timespan date="{entry_date}"}
    {if '{timespan:years}' < '3'}
        DOSTUFF
    {if:else}
        OTHERSTUFF
    {/if}
{/exp:timespan}

I'm not sure why their sample code includes references to {exp:ellapsed} - maybe it was an earlier name for the addon.

2
  • Thanks Dom, I did the same thing (look at the source code) and started by just trying to get it to output the years. When that failed I decided to stop bothering with an obviously poorly maintained plugin. – UltraBob Feb 18 '13 at 15:08
  • Ah, fair enough. I'll leave my answer up just to save anyone else wasting their time on that addon. – Dom Stubbs Feb 18 '13 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.