douchun1948 2014-04-15 09:48
浏览 29

如果日期在7天内,php显示红色div,否则显示其他颜色div?

I have a script that tells the user when something is about to expire in the database, so for instance if something expires on the 20th april, and today is the 15th, then it will tell the user there are 5 days left until it expires.

Now i want to add to my script to say if there are 7 days or less till expire date, then show a red coloured div, else if there are less than 30 days but more than 7, show a yellow coloured div, and otherwise if there are more than 30 days left till something expires, show a green coloured div.

can someone please show me what i could do, thanks

<?php 
    include 'config.php';
    $data = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date
                            FROM supplier_stats") or die(mysql_error());
?>

<?php 
    include 'config.php';
    $result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date 
                             FROM supplier_stats") or die(mysql_error()); 
    while($row = mysql_fetch_array($result)) 
    {
        $days = $row['expire_date'] -1;
        if ($days > 0)
        {
            echo "<p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p>"; 
        }
        else
        {
            $when = $days*-1;           
            echo "<p>Insurance expires";
            if ($when > 1){
                echo " in {$when} days</p>";
            }
            elseif ($when ===1)
            {
                echo " tomorrow</p>";
            }
            elseif ($when > 0)
            {
                echo " today</p>";
            }
        }
    } 
?>
  • 写回答

1条回答 默认 最新

  • dqu92800 2014-04-15 09:52
    关注

    Something like:

    <?
      if ($when <= 7) $color = "red";
      else if ($when <= 30) $color = "yellow";
      else $color = "green";
      echo "<div style='background-color: {$color};'>Expires in {$when} day(s)</div>";
    ?>
    

    Obviously, you would need to adjust this to fit your code.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条