tags:

views:

59

answers:

1

Related:

This is a continuation from my earlier question, Wrong Logic in If Statement?

$repeat_times = mysql_real_escape_string($repeat_times);

$result = mysql_query("SELECT `code`,`datetime` FROM `fc` ORDER by datetime desc LIMIT 25") or die(mysql_error());

$output .="";
$seconds = time() - strtotime($fetch_array["datetime"]);

if($seconds < 60)
  $interval = "$seconds seconds";
else
 if($seconds < 3600)
     $interval = floor($seconds / 60) . " minutes";
else
    if($seconds < 86400)
         $interval = floor($seconds / 3600) . " hours";
    else
         $interval = floor($seconds / 86400) . " days";

while ($fetch_array = mysql_fetch_array($result)) {

 $fetch_array["code"] = htmlentities($fetch_array["code"]);
 $output .= "<li><a href=\"http://www.***.com/?code=" . htmlspecialchars(urlencode($fetch_array["code"])) . "\" target=\"_blank\">" . htmlspecialchars($fetch_array["code"]) . "</a> (" . $fetch_array["datetime"] . ") </li>";


}

$output .="";

return $output;

Okay, fixed last problem. Now it's just outputtign the [DateTime] function when I want it to output :

LINK (5 Seconds Ago)
LINK (1 Minute Ago)
etc.

It's doing: janice (2009-08-05 14:02:01)

Please advise?

+1  A: 

You are performing your calculation outside the loop. Move it inside the loop, like this:

$result = mysql_query("SELECT `code`,`datetime` FROM `fc` ORDER by datetime desc LIMIT 25") or die(mysql_error());
$output = "";

while ($fetch_array = mysql_fetch_array($result))
{
    $code = htmlentities($fetch_array["code"]);

    $seconds = time() - strtotime($fetch_array["datetime"]);
    if($seconds < 60)
        $interval = "$seconds seconds";
    else if($seconds < 3600)
        $interval = floor($seconds / 60) . " minutes";
    else if($seconds < 86400)
        $interval = floor($seconds / 3600) . " hours";
    else
        $interval = floor($seconds / 86400) . " days";

    $output .= "<li><a href=\"http://www.***.com/?code=" . urlencode($code) . "\" target=\"_blank\">$code</a> ($interval) </li>";
}

return $output;
Jon Benedicto