1

Okay, so, I have a list table with 2 columns: codes and dates.

I want to display the LATEST 25 AND tell the user how long ago they were submitted.

So, for example:

ABCDEF (1 Second Ago)
CCDEE (12 Seconds Ago)
329492 (45 Minutes Ago)

I've gotten this far:

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

but, it doesn't do what I want. It does the reverse. It shows what was inputted FIRST, not LAST.

My output looks like this:

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

I have NO Idea how to add the (time since) part.

Help?

Thanks :)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663

5 Answers5

2

Try using

order by datetime desc

Then in PHP grab the current time, subtract the time returned from the query, and then take a look at this SO question about relative time to display your time in the proper units.

Community
  • 1
  • 1
Brandon
  • 68,708
  • 30
  • 194
  • 223
2

Consider ORDER BY datetime DESC to sort in the other direction.

Consider adding datetime to the SELECT list, so you can access the posting date in PHP. You can then use PHP date/time functions to calculte the difference between the current date, and the date posted, to work out how long ago the posting was posted.

Added: a bit of code to calculate the time since the posting in a friendly format.

$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";
 // You can keep on going

At the end $interval contains a textual representation of the interval

Jason Musgrove
  • 3,574
  • 19
  • 14
  • @Frank: Unfortunately, SO's commenting doesn't preserve line breaks, and has made your code a little less readable than it should be. *If* I've read it right, your last line looks syntactically incorrect. Where you have `"($fetch_array["datetime"]"";` it should probably read something like `(" . $fetch_array["datetime"] . ")";`. Additionally, are you copying the the result of my code back into `$fetch_array["datetime"]`? – Jason Musgrove Aug 05 '09 at 22:42