1

我正在尝试使用 DateTime 来确定新发布的漫画应该突出显示多长时间。所以,我的目标是让最新的漫画“突出显示”2 天,然后恢复正常(灰色,和其他人一样)。

注意:这是我之前问过的一个问题。我正在重申它,因为前面的问题对人们来说变得相当混乱。所以我重新编写了代码,简化了我的问题,并重新提问。

在此处输入图像描述

我的逻辑:

Loop through all comics {
   if comic date >= current date, display that comic with highlight CSS tag, 

   else display it with normal CSS tag.
}

我的代码:我想知道为什么这不起作用......它甚至没有显示最新的漫画(其日期> current_date)。

        $desc = (isset($_GET['description']) ? ($_GET['description']) : null);  


    $row = $catResult->fetch_assoc();

    $current_date = new DateTime;
    echo "Current Date: " . $current_date->format('Y-m-d H:i:s');

    //$comic_date->modify('3 day');

    //DISPLAY IMAGES TO CORRECT PAGE FROM DATABASE  

            echo '<ul>';
        $imageCounter = 0;
        while (($imageCounter < $imagesPerPage) && ($row = $catResult->fetch_assoc())) {

            $comic_date = new DateTime($row['date']);

            $class = ($comic_date >= $current_date) ? 'newcomics' : 'comics';   
                echo '<li>';                
                    echo        '<span class="' . $class . '"><a href=".?action=viewimage&site='.$site. '&id=' . $row['id'] .'" title="' . $row['description'] . '" alt="' . $row['title'] . '">
                                <img src="./scripts/thumber.php?img=.' . $thumbpath.$row['thumb'] . '&mw=220&mh=220"/></a> 
                                <br /><br /> ' . $row['description'] . $row['date'] . '</span>';                                
                    $imageCounter++;
                echo '</li>';


            }   
        echo '</ul>';   

有什么想法吗?

4

1 回答 1

2

您的三元条件已设置为条件为真,您只输出<span class="newcomics">而不是其他任何内容(不是锚标记等)。

我建议这样做以使其更具可读性:

$class = ($row['date'] >= $current_date) ? 'newcomic' : 'comic'

echo '<span class="' . $class . '"><a href=".?action=viewimage&site='.$site. '&id=' . $row['id'] .'" title="' . $row['description'] . '" alt="' . $row['title'] . '"><img src="./scripts/thumber.php?img=.' . $thumbpath.$row['thumb'] . '&mw=220&mh=220"/></a> <br /><br /> ' . $row['description'] . $row['date'] . '</span>';
于 2013-01-09T02:38:51.630 回答