简体   繁体   中英

php/mysql display number of days left until a date?

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.

My script then tells the user how many days are left until that date, ie 4 days left until insurance expires, or 3 days left until insurance expires.

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.

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

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM