2017-02-18 28 views
0

我只是需要一個提示功能,顯示以前的格式的時間。 在我的數據庫中,我有時間作爲時間戳記。有來自用戶的評論作爲時間戳。這個日期需要及時轉換。我有一個功能,但不能爲每個評論召回。它只有1次發表1條評論。有人可以幫忙嗎?php時間前功能

這是我的功能

function humanTiming($time) 
{ 

$time = time() - $time; // to get the time since that moment 
$time = ($time<1)? 1 : $time; 
$tokens = array (
    31536000 => 'year', 
    2592000 => 'month', 
    604800 => 'week', 
    86400 => 'day', 
    3600 => 'hour', 
    60 => 'minute', 
    1 => 'second' 
); 

foreach ($tokens as $unit => $text) { 
    if ($time < $unit) continue; 
    $numberOfUnits = floor($time/$unit); 
    return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':''); 
} 

} 
+0

你轉換時間戳劃時代它傳遞給'humanTiming前()' –

+0

不,我不@DanielSmith –

+0

是你的權利@GautamKrishnaR但功能不能重新聲明2個註釋例如。 –

回答

0

這可能是重複的,但關閉它不會告訴你在你的代碼是什麼問題。

return statement將立即從函數中返回傳遞的值,並結束函數的執行。所以你永遠不會超過你的foreach循環的第一次運行。也許你想要做的就是這樣的事情,在那裏你建立一個字符串中的循環,然後返回它:

$ret = ""; 
foreach ($tokens as $unit => $text) { 
    if ($time < $unit) continue; 
    $numberOfUnits = floor($time/$unit); 
    $ret .= $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':''); 
} 
return $ret; 

我其實沒有檢查你的代碼會工作,但是這是問題的關鍵你的問題。