We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

How to assign class if entry is less than 1 month old?

Development and Programming

Mixpo's avatar
Mixpo
25 posts
14 years ago
Mixpo's avatar Mixpo

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}
       
Sean C. Smith's avatar
Sean C. Smith
3,818 posts
14 years ago
Sean C. Smith's avatar Sean C. Smith

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.

       
Ian from Aus's avatar
Ian from Aus
94 posts
14 years ago
Ian from Aus's avatar Ian from Aus

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.

       
Mixpo's avatar
Mixpo
25 posts
14 years ago
Mixpo's avatar Mixpo

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?

       
Ian from Aus's avatar
Ian from Aus
94 posts
14 years ago
Ian from Aus's avatar Ian from Aus
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"' : ''); ?>
       
Mixpo's avatar
Mixpo
25 posts
14 years ago
Mixpo's avatar Mixpo

Ah yes. Ok, great, thanks for all your guys’ help!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.