0

Is it possible to modify a date variable (entry_date in this case) so that you have entry_date plus one year (or any other amount of time)?

For example, if entry_date was 6/4/14 2:59 PM, could I add one year to that date and end up with 6/4/15 2:59 PM, and would I still be able to use that variable in a conditional statement?

EDIT: Essentially what I want is something like this:

{if (entry_date + one_year) > current_time}
    <p>New Product</p>
{/if}

SECOND EDIT:

Robson, I tried the code that you added (both the regular and inverted logic) and I'm unfortunately STILL ending up with the same error. However, I continued to play around with the code and found that this does not throw an error:

{if {current_time format="%Y%m%d%H%i"} == {entry_date format="%Y%m%d%H%i"}}
    less than an year
{if:else}
    more than an year
{/if}

But this does throw the same error:

{if {current_time format="%Y%m%d%H%i"} - {entry_date format="%Y%m%d%H%i"} == 1}
    less than an year
{if:else}
    more than an year
{/if}

Do I need to change the comparative number to something more specific? Are we comparing two variables that we can't compare? Or am I just missing some bracket again? I feel like it's almost there...

  • Please, give me a little more detail on the conditional you need, so I can try to help you. – Robson Sobral Nov 23 '14 at 0:55
  • I made an edit above you so could see the code more easily. – B Mack Nov 24 '14 at 17:42
  • Are you using which version of EECMS? – Robson Sobral Nov 25 '14 at 0:17
  • I'm using ExpressionEngine v2.8.1 – B Mack Nov 25 '14 at 16:24
  • Mathematical operators works just on 2.9+. – Robson Sobral Nov 25 '14 at 18:10
0

I never tried something like this, but maybe it could work.

You can't add a year to a date without an add-on., but there are mathematical operators on the conditionals.

Format the date like a number using all data required by the precision you need plus one year following the same format: YYYYMMDDHHMM.

{if {entry_date format="%Y%m%d%H%i"} + 100000000 > {current_time format="%Y%m%d%H%i"} }
    less than an year
{if:else}
    more than an year
{/if}

Maybe it's better to invert the logic to make the code easier to understand:

{if {current_time format="%Y%m%d%H%i"} - {entry_date format="%Y%m%d%H%i"} < 100000000  }
    less than an year
{if:else}
    more than an year
{/if}

Using an add-on

If you can't update to EECMS 2.9 you can code your own add-on for this using strtotime() php function, like Low Nice Date does.

{exp:low_nice_date date="{entry_date format='%Y-%m-%d %H:%i + 1 Year'}" format="%Y%m%d%H%i"}

But the code will become far more complex and harder to maintain.

  • I tried this code, but replaced the XXX with current_time, added a close bracket, and closed the conditional statement, but I ended up with this error -- Parse error: syntax error, unexpected 'FALSE' (T_STRING) in /home/constbookbni/domains/constructionbook.bnibooks.com/public_html/ee/expressionengine/libraries/Functions.php(689) : eval()'d code on line 33 – B Mack Nov 24 '14 at 17:57
  • Did you notice my code has an extra )? I'm sorry about it! – Robson Sobral Nov 24 '14 at 18:19
  • I just tried the modified code in your post and I'm still throwing the same error. – B Mack Nov 24 '14 at 19:07
  • I wrote the name of the variable wrong again. I'm really, really sorry! I tested the code exactly how it is now and it works fine. – Robson Sobral Nov 24 '14 at 22:50
  • It almost works...I edited my original post for a more detailed explanation. – B Mack Nov 24 '14 at 23:17

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.