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 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 ?>

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