【问题标题】:how to highlight a cell of a row on expiration date [duplicate]如何在到期日期突出显示一行的单元格[重复]
【发布时间】:2014-02-06 11:56:21
【问题描述】:

您好,我搜索了很多内容,以找到一个脚本,可以为从今天到 +2 周的日期着色。今天之间的所有日期(例如 2014-02-06 和 2014-02-20)都应标记为红色。过去的日期也必须标记为橙色。所有其他日期保持未加密。我找不到让它工作的脚本,而且我对我的知识没有想法

<TABLE>
<TR><TD>item 1</TD><TD>2014-02-12</TD></TR>
<TR><TD>item 2</TD><TD>2014-06-17</TD></TR>
<TR><TD>item 3</TD><TD>2014-01-12</TD></TR>
<TR><TD>item 4</TD><TD>2015-08-12</TD></TR>
</TABLE>

我尝试过类似的方法,但这不起作用....

// get two weeks from now
$date_in_two_weeks = strtotime('+2 weeks');
$date_in_two_weeks = date("Y/m/d",$date_in_two_weeks);

// get the date to compare, from db or whatever you want
$date_to_compare = "2014/02/01";

// compare the date in your list to now + 2 weeks and then put the date difference into $days_difference
$date_from_list = new DateTime($date_to_compare);
$date_in_two_weeks = new DateTime($date_in_two_weeks);
$days_difference = $date_from_list->diff($date_in_two_weeks);

if ($days_difference->days > 14) {
    $highlight_css_class = "highlight";
} else {
    $highlight_css_class = "";
}

【问题讨论】:

  • 如何将其呈现为表格?
  • 该表只是在 html 中,就像我的问题中的表一样,但真的很大 600 行。我只想突出显示过去的日期和今天的日期 + 2 周。 PHP JQUERY JAVASCRIPT 与什么代码语言无关。但我无法让它工作
  • 我问的原因是,您可以在呈现值的位置突出显示它..
  • 日期不断变化,每天都在表格中呈现这种格式,但只是纯html。
  • 究竟什么是行不通的?从您粘贴的代码中,如果您不将检测到的类添加到每一行,它将永远无法工作,这意味着:>....

标签: javascript php jquery css


【解决方案1】:

试试这样的:

// get the date to compare, from db or whatever you want
$date_to_compare = "2014/02/02";
//today
$dateNow = new DateTime("now");
//date to compare
$dateCompare = new DateTime($date_to_compare);
//lets find difference between today and date to compare:
$difference = $dateNow->diff($dateCompare);

//for debugging; %R gives you prefix (- or +); %a gives you days.
//source: php.net/manual/en/datetime.diff.php
echo "Days in difference (from today until compare date): " . $difference->format('%R%a') . "<br />";

if ($difference->format('%R%a') < 0) {
    $highlight_css_class = "orange"; //If difference is less than 0.
} elseif ($difference->format('%R%a') <= 14) {
    $highlight_css_class = "red" //"Mark this date Red, date is in less than 2 weeks (14 days)";
} else {
    $highlight_css_class = "";
}

然后在你的代码中你像这样使用 $highlight_css_class:

<td class="<?php echo $highlight_css_class; ?>">...</td> 

【讨论】:

    【解决方案2】:

    我没有要评论的点,所以我必须写一个答案。请注意,我没有正确的答案,但我可能有一些想法可以帮助您朝着正确的方向前进。

    首先。尝试提供所有 TR 的 ID(如果可能)和班级名称!

    现在获取当前日期:

    var d = getDate();
    

    这将为您提供自 1970 年 1 月 1 日以来以毫秒为单位的日期和时间。现在 2 周有 1 209 600 000 毫秒(将其存储为 var 2weeks),所以现在您可以进行比较!

    if ( "time in ms to compare" < d)    //    Action to be taken for events in the past
    {
        var class = document.getElementsByClassName("className");
        for (var i=0; i<class.length; i++)
        {
            class[i].className = "before_today";
        }
    }
    else if ("time in ms to compare" >= d && "time in ms to compare" <= d + 2weeks)    // Action to be taken for events within the next 2 weeks.
    {
        var class = document.getElementsByClassName("className");
        for (var i=0; i<class.length; i++)
        {
            class[i].className = "within_2weeks";
        }
    }
    else    // Action to be taken (or not to be taken) for events more than 2 weeks away
    {
        var class = document.getElementsByClassName("className");
        for (var i=0; i<class.length; i++)
        {
            class[i].className = "above_2weeks";
        }
    }
    

    现在应该已经为不同的事件设置了类,现在在 CSS 中完成其余的工作。

    .before_today
    {
        background-color: rgba(255, 204, 0, 0,4)    //Orange with a 40% opacity
    }
    
    .within_2weeks
    {
        background-color: rgba(170, 0, 0, 0,4)    // Dark red with a 40% opacity
    }
    
    .above_2weeks
    {
        background-color: rgba(0, 170, 0, 0,4)    // Dark green with a 40% opacity
    }
    

    但是,这需要您将要比较的日期转换为自 1970 年 1 月 1 日以来的 ms,以便进行比较。自 1970 年 1 月 1 日以来,ms 的优点在于,即使有闰年和跨月,也可以很容易地比较日期(例如,一个日期应该是 1 月 30 日,另一个日期应该是今天)。

    这只是我短暂的头脑风暴。如果您能立即充分利用它,我会感到惊讶,但希望它可以让您朝着可能真正奏效的方向前进。

    其他人,请随时评论和/或更正我的意见。

    • 莫勒

    【讨论】:

      【解决方案3】:

      这是一个完整的示例。

      看看这个小提琴:http://phpfiddle.org/main/code/2xu-h6d

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <title>Sample</title>
              <style type="text/css">
                  .red { 
                      color : #f00; 
                  }
                  .orange { 
                      color : #ffa500; 
                  }
              </style>
          </head>
          <body>          
              <table>
                  <?php 
      
                  $dates   = array('2014-01-23', '2014-02-12', '2014-06-17', '2014-01-12', '2015-08-12');
                  $dateNow = new DateTime("now");
      
                  foreach ($dates as $date):    
                      // Compare the date in your list to now
                      $dateFromList   = new DateTime($date);
                      $daysDifference = $dateNow->diff($dateFromList);
      
                      $class = '';
      
                      if (((int) $daysDifference->format('%r%a')) < 0):
                          $class = 'orange';
                      elseif (((int) $daysDifference->format('%r%a')) <= 14):
                          $class = 'red';
                      endif;
      
                      echo '<tr class="', $class, '"><td>', $date, '</td></tr>';
      
                  endforeach;
                  ?>
              </table>
          </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2015-09-29
        • 1970-01-01
        • 2013-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多