简体   繁体   中英

Need help with mysql/php outputting data

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 :)

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.

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

If I understand right the question the problem is that you don't specify the order of sort. If you want to obtain the latest posts you have to specify the descendant order.

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

To fix the ordering:

"SELECT `code`, `datetime` FROM `fc` ORDER by datetime DESC LIMIT 25"

To get time diff, something like this should work. Note that you should refactor this into better methods, remove the "magic number" etc. (It can also be extended to be more sophisticated):

function getTimeAgo ($dateTime) {
    $timestamp = new DateTime($dateTime);
    $currentTimestamp = new DateTime();

    $diff = $currentTimestamp->getTimestamp() - $timestamp->getTimestamp();

    if($diff < 0) {
        throw new Exception (__METHOD__ . ':parameter $dateTime can not be 
                                           in the future!');
    }
    if($diff < 60) {
       return "$diff seconds ago";
    }
    if($diff < 3600) {
       return $diff/60 . " minutes ago";
    }
    if($diff < 86400) {
       return $diff/3600 . " hours ago";
    }
}

Changing datetime order: Try DESC or ASC at the end of your ORDER BY. This should do the trick:

SELECT code
FROM fc
ORDER BY datetime DESC
LIMIT 25

Time since: Write or find a PHP function that converts MySQL datetime format to 'real English'. Here's a quick basic example:

<?php

// your code goes here
$timeStr = "2009-08-01 15:43:34";
$time = Sec2Time( time() - strtotime($timeStr) );
print_r($time);

// this converts mysql datetime into english
//   borrowed from http://ckorp.net/sec2time.php
function Sec2Time($time){
  if(is_numeric($time)){
    $value = array(
      "years" => 0, "days" => 0, "hours" => 0,
      "minutes" => 0, "seconds" => 0,
    );
    if($time >= 31556926){
      $value["years"] = floor($time/31556926);
      $time = ($time%31556926);
    }
    if($time >= 86400){
      $value["days"] = floor($time/86400);
      $time = ($time%86400);
    }
    if($time >= 3600){
      $value["hours"] = floor($time/3600);
      $time = ($time%3600);
    }
    if($time >= 60){
      $value["minutes"] = floor($time/60);
      $time = ($time%60);
    }
    $value["seconds"] = floor($time);
    return (array) $value;
  }else{
    return (bool) FALSE;
  }
}

?>

And the output is:

Array ( [years] => 0 [days] => 4 [hours] => 5 [minutes] => 29 [seconds] => 38 )

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM