PHP/MySql确定插入数据的年龄/运行时间


PHP/MySql Determining age/Elapsed Time of inserted data

老实说,我不知道如何解释这一点,所以如果你看看http://www.gw2lfg.com,逝去的时间列是我正在尝试做的,确定某个东西是一分钟前的还是两分钟前的,等等。感谢

的所有帮助

您可以执行以下操作来查找时间流逝。

 UNIX_TIMESTAMP('2012-12-01 12:12:12') - UNIX_TIMESTAMP(CURTIME()); 

然后可以将其转换为秒和分钟。

试试这个工作示例,

function time_ago($date)
    {
        if (empty($date)) { // $date=> mysql timestatmp
            return "No date provided";
        }      
        $periods    = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
        $lengths    = array("60","60","24","7");
        $now        = time();
        $unix_date  = strtotime($date);
        // check validity of date
        if (empty($unix_date)) {   
            return "Bad date";
        }
        // is it future date or past date
        if ($now >= $unix_date) {   
            $difference = $now - $unix_date;
            $tense      = "ago";
        } else {
            $difference = $unix_date - $now;
            $tense      = "from now";
        }
        for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
            $difference /= $lengths[$j];
        }
            $difference  = round($difference);
        if ($difference != 1 && $j != 0) {
            $periods[$j].= "s";
        }
        if($difference!=0)
        return "$difference $periods[$j] {$tense}";
        else
        return "a few seconds ago";
    }

更新:

用法:

echo time_ago('2013-07-01 11:30:00'); // Output: 5 mins ago