0

I am trying to automate the process of adding a css New icon next to my K2 article within the first 3 days of the published date. So here's what I came up with in com_k2\template\category_item.php

    $newdate = time($this->item->publish_up) + 86400;
    $nowdate = time();
    <?php echo $nowdate <= $newdate?' item-new':'' ?>

The first line will get the published date of the article and add 3 days to it. The 2nd line would then grab the current server time. Then the 3rd line would compare the two date and echo item-new if the current time is still within 3 days from the published date.

The code did not generate any error msg, but the comparing of the two date doesn't seems to work because the New icon display on every article.

Could someone kindly point me in the right direction? I'm not good with php and most of that coding was inferred from other code. My guess is that the variables were defined properly but I cannot simply compare two dates with smaller or equal to?

4

1 回答 1

0

It is possible to compare time in PHP using comparison operators. The problem is that time() doesn't take any arguments (PHP isn't throwing an error because it takes extra arguments as params, like JS). What you probably want to use is strtotime().

$newdate = strtotime($this->item->publish_up) + 86400;
$nowdate = time();
<?php echo $nowdate <= $newdate?' item-new':'' ?>
于 2012-07-30T16:10:30.750 回答