代码之家  ›  专栏  ›  技术社区  ›  ganjan

想将strtotime值与currentTime和output进行比较:大约在x分钟前

  •  2
  • ganjan  · 技术社区  · 15 年前

    我正在做一个类似于Facebook的新闻提要。在这里,我将mysql表中发生的事情与当前的时间和输出进行比较:x分钟前发生的事情。

    首先,我用以下代码连接到mysql:

            $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 回复  |  直到 15 年前
        1
  •  2
  •   Thomas Winsnes    15 年前

    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
    

    或者,您可以创建一个打印秒、分钟、小时等的函数

        2
  •  2
  •   Matthew    15 年前

    我用这个(改编自 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";
        }
    
    
    }