php过期日期红绿灯系统

标签 php mysql css

我正在研究交通灯系统。它有点乱,没有按应有的方式工作。

我的目标是让系统像这样工作;

数据库中有供应商保险到期的日期。如果到期日期是 30 天或更短,那么我希望这些日期被拉出并回显,但不应显示其他 30 天后到期的日期。

目前它还给出到期日之前的天数,但这不能正常工作,所以说今天是 5 月 2 日,明天是 3 日;保险文件将于 3 日到期,所以它应该说保险明天到期。这目前无法正常工作,我得到的是“2 天”直到到期而不是“明天到期”

同样明智的是,如果保险今天到期,它应该说今天到期,但日期不对。

此外,我还设计了不同颜色的 div 标签,根据保险文件的到期日期显示为一种交通信号灯。如果文档将在 30 到 20 天内到期,我希望显示我的绿色 div,否则如果文档将在 19-7 天内到期,我希望它显示为琥珀色,如果文档将在 7- 0 天或到期应为红色。

这是我的代码,请有人告诉我如何改进它来完成我需要它做的事情,谢谢

代码:

    <?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:995px;  font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
         font-size:11px;\" >

    <tr>
        <td style=\"width:100px;\">ID:</td><td>Company Name:</td><td>Company Reg No:</td><td>Owner:</td><td style=\"width:200px;\">Note:</td><td style=\"width:100px;\">Date:</td><td>Status:</td></tr>";


         while($row = mysql_fetch_array( $data )) { 
           $days = $row['expire_date'] -1;


           echo "<tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>"; 
           echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>"; 
           echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
           echo "<td style=\"width:100px;\"><p>".$row['owner'] . "</p></td>";

           if ($days > 0) {
                echo "<td style=\"width:200px;\"><p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p></td>"; 
            } else {
              $when = $days*-1;           

              echo "<td style=\"width:200px;\"><p>Insurance expires";

              if ($when > 1){
                  echo " in <font color=\"red\">{$when} days</font></p></td>";

              } elseif ($when < 1) {
                echo "<font color=\"red\"> tomorrow!</font></td>";
              }
            elseif ($when == 0) 
            {
                echo " today!</font></p></td>";
            }

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

      echo"</td>";


            }
            if ($when >= 20){
                echo "<td style=\"width:150px;\"><div class=\"green_light\"></div></td>";
              }

              if ($when >= 7){
                echo "<td style=\"width:150px;\"><div class=\"amber_light\"></div></td>";
              }

 if ($when <= 7){
                echo "<td style=\"width:150px;\"><div class=\"red_light\"></div></td>";
              }


            echo "<tr>";
          }


          echo "</table>"; //Close the table in HTML


        ?>

最佳答案

更改查询以准确反射(reflect) expiry_date 之前的天数,然后它应该简化检查。下面的查询截断到一天,并给出准确的 days_before_expires 作为正数。

正数是几天前。所以 +1 意味着明天结束。

零意味着今天结束

负数表示已过期。

SELECT *, TIMESTAMPDIFF(DAY,  CURRENT_DATE(), DATE(insurance_date)) AS days_before_expires FROM supplier_stats

转换为“mysqli”。

所有代码都经过测试。它实现了“交通灯”状态。我已经更改了格式代码以尝试通过使用函数来计算“紧急指示器”来使其更易于维护。所有格式现在都使用 css 完成。

<!DOCTYPE HTML">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>23424062/php-expiry-date-traffic-light-system</title>
    <style type="text/css">
        table {
            width:995px;
            font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
            font-size:11px;
        }
        th {text-align: left; }
        .width_small {
            width: 100px;
        }

        .width_medium {
            width: 150px;
        }

        .width_large {
            width: 200px;
        }
        .urgency_1 {
            display: inline-block;
            width: 6em;
            background-color: green;
            color: yellow;
            padding-left: 1em;
        }
        .urgency_2 {
            display: inline-block;
            width: 6em;
            background-color: orange;
            padding-left: 1em;
        }
        .urgency_3 {
            display: inline-block;
            width: 6em;
            background-color:  red;
            color: yellow;
            padding-left: 1em;
        }

        .green_light {
            width: 3em;
            background-color: green;
        }
        .amber_light {
            width: 3em;
            background-color: orange;
        }
        .red_light {
            width: 3em;
            background-color:  red;
        }
    </style>
  </head>

  <body>
<?php
$mysqli = new mysqli('localhost', 'test', 'test', 'testmysql');


$data = $mysqli->query("SELECT *, "
                       ." TIMESTAMPDIFF(DAY,  CURRENT_DATE(), DATE(insurance_date)) AS days_before_expires, "
                       ." DATE(insurance_date) as insurance_day "
                       ." FROM supplier_stats  order by DATE(insurance_date)")
  or die(mysql_error());
?>
<table>
<tr><th class="width_small">ID:</th><th>Company Name:</th><th>Company Reg No:</th><th>Owner:</th><th class="width_large">Note:</th><th class="width_small">Date:</th><th>Status:</th></tr>

<?php while($row = $data->fetch_array()): ?>
    <?php $daysLeft = $row['days_before_expires']; ?>
    <?php $urgency = getUrgency($daysLeft); ?>
    <?php $cssTrafficLight = selectUrgencyLit($row['days_before_expires'],
                                       array('', 'green_light', 'amber_light', 'red_light')); ?>
    <?php $cssUrgency = selectUrgencyLit($row['days_before_expires'],
                                       array('urgency_0', 'urgency_1', 'urgency_2', 'urgency_3')); ?>
    <?php $daysLeftLit = getDaysLeftLiteral($daysLeft); ?>

    <tr><td class="width_small"><p><?= $row['id'] ?></p></td>
    <td class="width_medium"><p><?= $row['company_name'] ?></p></td>
    <td class="width_medium"><p><?= $row['company_reg_number'] ?></p></td>
    <td class="width_small"><p><?= $row['owner'] ?></p></td>
    <td class="width_large"><?= $daysLeftLit; ?></td>

    <td class="width_small"><?= date('d/m/Y',strtotime($row['insurance_day'])) ?></td>
    <td class="width_medium"><div class="<?= $cssTrafficLight?>">&nbsp;</div></td>
    <tr>
  <?php endwhile; ?>
  </table> <!-- Close the table in HTML -->
  </body>
</html>

    <?php
/*
 * To tidy the code up and hopefully make it easier to maintain we need
 * a couple of functions...
 *
 * How about assigning an 'urgency level' to each record depending on the
 * days left before the expiry date. The level is as follows:
 *
 * 0 => not urgent  (more than 20 days)
 * 1 => need to pay attention (14 - 20)
 * 2 => soon (7 - 13)
 * 3 => now  (0 - 6)
 */
function getUrgency($daysLeft)
{
    if ($daysLeft >= 21 || $daysLeft < 0) { return 0; }
    if ($daysLeft >= 14  && $daysLeft <= 20) { return 1; }
    if ($daysLeft >=  7  && $daysLeft <= 13) { return 2; }
    return 3;
}

// return a literal depending on the urgency
function selectUrgencyLit($daysLeft, array $litArray)
{
    $urgency = getUrgency($daysLeft);
    if (!empty($litArray[$urgency])) {
        return $litArray[$urgency];
    }
    return '';
}

// return the days left literal
function getDaysLeftLiteral($daysLeft)
{
    $urgency = getUrgency($daysLeft);
    if ($urgency <= 0) {
       return '&nbsp;';
    }

     $text = "Insurance Expires: <div class=\"urgency_{$urgency}\">";

    // set the day literal to be shown
    if  ($daysLeft == 0) { $dayLit = 'today'; }
    elseif ($daysLeft == 1)  { $dayLit = 'tomorrow'; }
    elseif ($daysLeft >= 0 && $daysLeft <= 20)  { $dayLit = "in {$daysLeft} days"; }
    else { $dayLit = '&nbsp;'; }

    return $text . $dayLit .'</div>';
}

关于php过期日期红绿灯系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424062/

相关文章:

php - 方法 ob_start 和 ob_flush 不起作用,为什么?

php - 我可以在 PHP 中混合使用 MySQL API 吗?

php - 用户注册表提交后出现空白页

android - 使用本地 js/css 加载网站

html - 响应式背景图片 - 使用 Ruby on Rails Bootstrap jumbotron

php 给出奇怪的输出

php - 用于集成 PayPal IPN 的示例 php 代码

php - 使用php检查多个数据库和多个表的数据

php - 用 PHP 编写 MySQL 数据库查找/替换脚本

jquery - 如何设置div和图像的相同宽度