【问题标题】:error in highlight table dates突出显示表格日期错误
【发布时间】:2014-02-06 11:07:14
【问题描述】:

这段代码有什么问题?我想突出显示从现在 + 2 周后的所有日子。但是它们都被 .highlight 类突出显示。哪里出错了?

<?php 
// 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 = "none";
}
?>
<style>
.highlight { 
    color:#cc0000;
}
.none { 
}
</style>
<fieldset>
<table class="tablesorter" id="my-table" border="1" style="border-collapse:collapse">
        <thead>
            <tr>
                <th>REK</th>
                <th>BESCHRIJVING</th>
                <th>VERVALDATUM</th>
            </tr>
        </thead>
        <tbody>


<tr><td>A1</td><td>hamburger</td><td class="<?php echo $highlight_css_class;?>">2014-02-10</td></tr>

<tr><td>A1</td><td>tomato</td><td class="<?php echo $highlight_css_class;?>">2014-06-10</td></tr>


        </tbody>
    </table>

我测试了代码并进行了很多更改,但我无法让它工作.... 提前谢谢您的帮助。

【问题讨论】:

  • 您没有按日期进行任何比较,您只进行了一次比较。创建一个函数,并传入当前日期以确定突出显示类,或者您可以简单地在 jquery 中执行此操作。
  • 您总是比较“2014-02-01”,您想要比较“2014-02-10”和“2014-06-10”的日期从未在您的代码中比较过。
  • 我在我写的内容中找不到错误...你能给我一个可行的例子吗?
  • 不给课就检查,代码是否正常工作..可能是添加类的问题。
  • 目前有四个重复您自己的问题Highlight all dates from now untill 2 weeks later请停止此现在

标签: javascript php css


【解决方案1】:

试试这个

$date_in_two_weeks = strtotime('+2 week');

而不是

$date_in_two_weeks = strtotime('+2 weeks');

您必须写 week 而不是 weeks 才能获得下两周的正确时间戳

【讨论】:

  • 会因为差值大于 14。尝试将比较日期更改为 2014-02-15
猜你喜欢
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多