Link to home
Start Free TrialLog in
Avatar of Sim1980
Sim1980

asked on

How to figure out if a date is within 30 days in php

Hi all.

I have a php script that displays records in a table. I want to be able to show specific records in yellow when the field: InsuranceExpiration is within 30 days of expiration.

What is the correct php syntax to figure out when a date (i.e. InsuranceExpiration) is within 30 days?

Thank you in advance!
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This answer (and much more) about date and time is shown in this article.  PHP date() and strtotime() play together in ways that are almost magical.  To use them correctly keep all of your internal date/time representations in ISO-8601 format.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Be aware that PHP and MySQL keep the date and time separately.  You may want to verify how close or far apart the servers are.

$future = date('c', strtotime('TODAY + 30 DAYS'));
if ($insuranceExpiration < $future) $displayStyle .= ' yellow';
// etc... 

Open in new window