I’m trying to find a way to add a class to entries that are new (less than 1 month old). Anyone know of some code that will do this?
I think it’s something along these lines, but can’t quite figure it out…
{if {entry_date format='%Y-%m-%d'} < ({current_time format='%Y-%m-%d'} -something here)}
class="new-item"
{/if}Hi Mixpo,
To achieve this you will probably need to use one of the Math Plugins available on Devot-ee.
I’m moving this thread to development and programming since it’s more about template coding than problems with ExpressionEngine.
Feel free to post again as needed in the future.
The easiest way to calculate if a date is within a range of another is to format the dates with %U (seconds since Unix epoch) and then simply take off the size of your range and compare them, more or less as you have done.
Something like this (using 30 days as the time constraint - 606024*30):
{if {entry_date format='%U'} < ({current_time format='%U'} - 2592000)}
class="new-item"
{/if}The important question is can EE’s advanced conditionals do basic math?
This of course is also not going to match an exact month, based on the current time. The PHP date function strtottime() can take a textual date expression but that would require either some PHP or a specific plugin.
Thanks for the help guys. I was able to get it working using some PHP and Ian’s suggestion:
<?php if (
{entry_date format="%U"} + 2592000 >= {current_time format="%U"}
) echo 'class="new-item"' ?>Does this look like a good way to handle it, or could this code be optimized at all?
could this code be optimized at all?
You could do something like this, but it is virtually the same, and possibly more obfuscated depending on how comfortable you are with the syntax:
<?php echo ({entry_date format="%U"} + 2592000 >= {current_time format="%U"} ? 'class="new-item"' : ''); ?>Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.