views:

24

answers:

2

Hi. I am working on a newsfeed like similar to facebook. Where I compare the time when something happened in my mysql table to the current time and output: something something happened x min ago.

First i connect to mysql with this code:

        $conn = db_connect();
        $newsfeed = $conn->query("select info, username, time from newsfeed ORDER BY time DESC LIMIT 10");
        if (!$newsfeed) {
        //die(msg(0,"Could not execute query"));
        }

I am new to mysql so im not sure of this is correct. I want the last 10 updates in the database to show up with the next functions with the latest updates above the others:

        $newsfeed_info_array = $newsfeed->fetch_array(MYSQLI_NUM);
        $newsfeed_info = $newsfeed_info_array[0];   
        $newsfeed_username = $newsfeed_info_array[1];   
        $newsfeed_time= $newsfeed_info_array[2];
        $newsfeed_info_split = explode("-", $newsfeed_info);    //type and info

        date_default_timezone_set('Europe/Paris');
        $current_time = strtotime("now"); 

        if ($newsfeed_info_split[0] == "reg")
            {
                            //The next line is the problem
            $time_diff = date_diff($current_time, $newsfeed_time); 
            echo "<p>User $newsfeed_username just registerted ".date( "00:i:s", $newsfeed_time)." min ago</p><br>";  
            }

        if ($newsfeed_info_split[0] == "xxx")
            {
            echo "blabla x min ago"
            }
+2  A: 

Since you're using unix_timespan, it's only a matter of using basic maths :) Unix timespan is the number of seconds since January 1 1970 00:00:00 UTC. http://en.wikipedia.org/wiki/Unix_time


$postTime = $newsfeed_into_array[2]; 
$now = time();
$seconds = $now - $postTime; // $seconds now contains seconds since post time

$minutes = ceil($span / 60); // minutes since post time

Alternatively you can make a function that prints seconds, minutes, hours etc

Thomas Winsnes
thanks, now it works perfectly :)
ganjan
+1  A: 

I use this (adapted from Codeigniter Forums - Nicer Dates):

if( ! function_exists('relative_time'))
{
    function relative_time($datetime)
    {
        if(!$datetime)
        {
            return "no data";
        }

        if(!is_numeric($datetime))
        {
            $val = explode(" ",$datetime);
            $date = explode("-",$val[0]);
            $time = explode(":",$val[1]);
            $datetime = mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
        }

        $difference = time() - $datetime;
        $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
        $lengths = array("60","60","24","7","4.35","12","10");

        if ($difference > 0)
        {
            $ending = 'ago';
        }
        else
        {
            $difference = -$difference;
            $ending = 'to go';
        }
        for($j = 0; $difference >= $lengths[$j]; $j++)
        {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);

        if($difference != 1)
        {
            $period = strtolower($periods[$j].'s');
        } else {
            $period = strtolower($periods[$j]);
        }

        return "$difference $period $ending";
    }


}
Matthew