Skip to content
Advertisement

Can’t write the right if statement regarding time

I have a database with rows of employee information. In the data I have training dates that expire. I need to compare the expiration date that is pulled to the current date. I’d like to bold and turn red the date if it is within 30 days of expiring. So anytime the page is loaded if it is within that 30 day time frame it will stand out.

This is what I have currently without the if statement I am looking for:

$lic_ex = date("m/d/Y", strtotime($row['lic_ex']));
echo "<td>" . $lic_ex . "</td>";

Advertisement

Answer

  1. Convert expiration date in DateTime object.
  2. Create DateTime object which represents today date + 30 days. $thirtyDaysLater = (new DateTime('today'))->modify('+30 day');
  3. Compare both objects and if DateTime object from 2. is bigger, then the person is in 30 days expiration period and you can bold the text For example:
$expirationDate= DateTime::createFromFormat('m/d/y' , $input);
$compareDate = (new DateTime('today'))->modify('+30 day');

if ($compareDate >= $expirationDate) {
    echo "<td><strong>" . $lic_ex . "<strong></td>";
} else {
    echo "<td>" . $lic_ex . "</td>";
}

just adjust the createFromFormat parameters to your data

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement