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 KennyH - October 29, 2018

Hi Daniel,

The output with the way the statements are written now shows just a grin-beam (15 Days)

I changed 15 days to < instead of > and a 90+ day entry jumped to 20. So, I switched 20 to < instead of > and it worked. I ran some other tests and it seems to be running good that way.

<?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 elseif (time() - strtotime($record['updatedDate']) > 60*60*24*30): ?>
<i class="fal fa-frown-open fa-lg text-danger"></i>

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

<?php endif ?>