i want to create a traffic lights system that will tell me how many days are left to go until a document reaches its expiration date. I want it to be marked by colors in my html.
- green: if the document has 31 days or more to go until it expires.
- amber: 30 days or less until it expires.
- red: for 7 days or less.
I also want to display a text, telling me how many days until expiration. The expiration date comes from my database in the field called insurance_date
Heres my code i am new to php so struggling to put my code together, if someone could show me how to restructure my code in order to achieve what im trying to do id be greatful, thanks.
my code:
<?php
include 'config.php';
$data = mysql_query("SELECT * FROM supplier_stats")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo "<p>Insurance Expires ! - <font color=\"red\">".$info['insurance_date'] . "</font></p>";
}
$date_diff = 60*60*24*7; // 7 days
// time()+$date_diff will be a timestamp 7 days in the future
if ($info['insurance_date'] < time()+$date_diff) {
echo "less than 7 days left!";
}
In your while
loop, you iterate over all entries in the supplier_stats
table.
For every iteration, you output "<p>Insurance Expires ! ...
.
You probably do not want to do that.
Edit your query to only select the entries that will expire within your range.
Example:
SELECT * FROM supplier_stats WHERE insurance_date >= yourDateToCompareTo
Depending on how you did set up your Database, insurance_date
may be of 'MySQL Type DATE or String or even Integer. Depending on that, you have to make the Comparsion to check if the date is close to your expiration range.
After your loop (closed by }
) you still use the $info
in your if ($info['insurance_date']
which will only use the LAST entry in your table.
This is however a very, very basic Question and i'd advise you to read up on some very basic beginners tutorials which can be found easily using an internet search engine.
I'm guessing $info['insurance_date']
is a datetime string in the format YYYY-mm-dd HH:ii:ss
, right?
If this is the case, you will need to run it through strtotime
before comparing it to a numeric time (time()+$date_diff
)
来源:https://stackoverflow.com/questions/23060541/basic-date-comparsion-for-expiration-checking