if statement for days elapsed

4 posts by 2 authors in: Forums > CMS Builder
Last Post: October 30, 2018   (RSS)

By KennyH - October 26, 2018

I am needed a little help isolating this down a little further. I am trying to get a specific icon to show up depending on how much time has elapsed since the createdDate

If 15 days or less show fa-grin-beam

If 20 days or less show fa-grimace

If 30 days or less show frown-open

My problem is that when it is 20 days past, both 15 & 20 icons show up. If 30 days has elapsed 15, 20, & 30 icons show up. How can I get just the appropriate icon to show up while ignoring the others?

<?php if (time() - strtotime($record['createdDate']) > 60*60*24*15): ?>
<i class="fal fa-grin-beam fa-lg text-default"></i>

<?php elseif (time() - strtotime($record['createdDate']) > 60*60*24*20): ?>
<i class="fal fa-grimace fa-lg text-info"></i>

<?php elseif (time() - strtotime($record['createdDate']) > 60*60*24*30): ?>
<i class="fal fa-frown-open fa-lg text-warning"></i>

<?php endif ?>

Kenny H

By daniel - October 29, 2018

Hi Kenny,

Do you have any other related code that could be outputting icons? An if/elseif chain like in your post should only ever output one result, so having a problem with more than one icon showing up is unexpected.

It also looks like you have your comparisons backwards (> instead of <), meaning that fa-grin-beam will show up after 15 days rather than at 0-15 days. If you switch those operators - and there isn't any other icon code - it looks like it should be doing what you want.

Let me know if you have any more questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By daniel - October 30, 2018

Hi Kenny,

Based on the code above, you should get the following results:

  • Between 0-15 days: grin-beam
  • Between 15-20 days: grimace
  • Over 20 days: frown-open

If that's the result you want, you can simplify it a bit further by removing the 30-day "elseif" so it looks something like this:

<?php if (time() - strtotime($record['updatedDate']) < 60*60*24*15): ?>
<i class="fal fa-grin-beam fa-lg text-default"></i>

<?php elseif (time() - strtotime($record['updatedDate']) < 60*60*24*20): ?>
<i class="fal fa-grimace fa-lg text-warning"></i>

<?php else: ?>
<i class="fal fa-frown-open fa-lg text-danger"></i>

<?php endif ?>

Cheers,

Daniel
Technical Lead
interactivetools.com