【问题标题】:php expiry date traffic light systemphp到期日期红绿灯系统
【发布时间】:2014-05-02 08:41:10
【问题描述】:

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

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

数据库中有供应商保险到期的日期。如果到期日期是 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


        ?>

【问题讨论】:

    标签: php mysql css


    【解决方案1】:

    更改查询以准确反映 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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多