2010-01-03 88 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"