php / mysql显示剩余的天数直到日期? [英] php/mysql display number of days left until a date?
问题描述
我正在使用这个脚本,通知你有多少天直到特定的日期,在我的数据库中有一个名为insurance_date的表是DATETIME格式的,这是一个唯一的到期日。
i am using this script which notifys you of how many days are left until a specific date, in my database there is a table called 'insurance_date' which is in DATETIME format and in that is a unique expiry date.
然后,我的脚本告诉用户直到该日期剩余多少天,即保险到期前剩余4天,直到保险到期为止3天。
My script then tells the user how many days are left until that date, i.e 4 days left until insurance expires, or 3 days left until insurance expires.
它还使用红色或绿色的交通指示灯,因此如果直到有效期为7天或以下,则会显示红色,否则如果8天或更多天将显示绿色。
It also uses red or green traffice light indicators, so if days until expiry are 7 or under it will show red, else if 8 or more days it will show green.
目前脚本工作正常,但最多只有2天,然后脚本假定说明天有保险到期,如果有1天要去,或者如果到期,那么今天日期是今天的日期。
At the moment the script works fine but only upto '2 days' and then the script is suppose to say 'insurance expires tomorrow, if there is 1 day to go, or 'today' if the expiry date is today's date.
有人可以告诉我我在做错什么,谢谢
can someone please show me what i am doing wrong, thanks
代码: / p>
code:
<?php include 'config.php';
$data = mysql_query("SELECT *, TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date FROM supplier_stats")
or die(mysql_error());
echo "<table class=\"table\" style=\"width:900px; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px;\" >
<tr>
<td style=\"width:150px;\">ID:</td><td>Company Name:</td><td>Note:</td><td>Status:</td></tr>";
while($row = mysql_fetch_array( $data )) {
$days = $row['expire_date'] -1;
echo "<tr><td style=\"width:150px;\"><p>".$row['id'] . "</p></td>";
echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>";
if ($days > 0) {
echo "<td style=\"width:150px;\"><p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p></td>";
} else {
$when = $days*-1;
echo "<td style=\"width:150px;\"><p>Insurance expires";
if ($when > 1){
echo " in {$when} days</p></td>";
}
if ($when >= 8){
echo "<td style=\"width:150px;\"><div class=\"green_light\"></div></td>";
}
if ($when <= 7){
echo "<td style=\"width:150px;\"><div class=\"red_light\"></div></td>";
} elseif ($when > -1) {
echo " tomorrow</p></td>";
} elseif ($when === 0) {
echo " today</p></td>";
}
}
echo "<tr>";
}
echo "</table>"; //Close the table in HTML
?>
推荐答案
我不知道你在数据库中的存储但是假设您正在存储保险到期日期的时间戳,我将执行此操作。
I dont know what you are you storing in the database but supposing you are storing the timestamp of the date when the insurance expires I would do something like this.
$expiry_date=$row['expire_date'];
$current_date=time();
$seconds_to_expire=$expiry_date-$current_date;
$days_to_expire=floor($seconds_to_expire/86400);
if($days_to_expire<=0)
{
echo "Expired";
}
else if($days_to_expire==1)
{
echo "Expires tomorrow";
}
else if($days_to_expire==2)
{
echo "Expires in two days";
}
else
{
echo "Expires in ".$days_to_expire." days";
}
这篇关于php / mysql显示剩余的天数直到日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!