2010-01-03 91 views
0

這裏是我的PHP代碼.....PHP時間差腳本問題

<?php 
function nicetime($date) 
{ 
    if(empty($date)) { 
     return "No date provided"; 
    } 

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

    $now    = time(); 
    $unix_date = strtotime($date); 

     // check validity of date 
    if(empty($unix_date)) {  
     return "Incorrect Date"; 
    } 

    // is it future date or past date 
    if($now > $unix_date) {  
     $difference  = $now - $unix_date; 
     $tense   = "ago"; 

    } else { 
     $difference  = $unix_date - $now; 
     $tense   = "ago"; 
    } 

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { 
     $difference /= $lengths[$j]; 
    } 

    $difference = round($difference); 

    if($difference != 1) { 
     $periods[$j].= "s"; 
    } 

    return "$difference $periods[$j] {$tense}"; 
} 
?> 

,但它沒有顯示的時間正確....如果有任何當前時間是考慮到它顯示25- 35秒前等等....但我想像一個像2個薄荷糖和3秒前格式的像digg.com一樣的時差函數...我怎麼能這樣做

回答

1

試試看吧另一種方式 - 在開始時乘以所有$lengths值(即$multiplied)。
1.用此值除以$difference
2.如果> = 1,那麼你有一個解決方案。
否則將$multiplied除以array_pop($lengths)的值。轉到2.
3.您有解決方案。

我認爲就是這樣。

順便說一句 - $tense總是"ago"