2010-08-12 41 views
2
if($timestamp > time() - 60){ 
    // Count seconds ago 
    $deff = time() - $timestamp; 
    $seconds = $deff/60; 
    echo intval($seconds)." seconds ago"; 
} elseif($timestamp > time() - 3600){ 
    // Count minutes ago 
    $deff = time() - $timestamp; 
    $minuts = $deff/60; 
    echo intval($minuts)." minutes ago"; 
} 

我覺得分鐘前是正確的,但不是秒?我應該如何做「秒前」的權利?PHP:分鐘前,秒前:這是正確的編碼?

+0

像[日期/時間](http://us.php.net/DateTime)這樣的軟件包一般可以幫助你。哦,'$ deff' *是*秒。 – 2010-08-12 18:40:13

+1

我在這裏錯過了什麼嗎?使用'string date(string $ format [,int $ timestamp])''有什麼問題? – Incognito 2010-08-12 18:41:53

回答

3

不要除以60 該值以秒爲代表的已經,你不需要把它轉換成秒。

這段代碼有點亂。考慮使用DateTime包;它具有內置的差異和格式化功能。

0

我認爲你的'$秒'應該等於'$ deff',不是嗎?

+1

這更像是一個評論,而不是答案... – Makoto 2014-03-02 18:56:11

0

我使用這個...它處理過去和未來的時間。

/** 
* Get a string representing the duration difference in two timestamps 
* 
* @param int $time The timestamp to compare 
* @param int $timeBase The base timestamp to compare against, defaults to time() 
* @return string Returns the duration summary 
*/ 
public function getTimeSummary ($time, $timeBase = false) { 
    if (!$timeBase) { 
     $timeBase = time(); 
    } 

    if ($time <= time()) { 
     $dif = $timeBase - $time; 

     if ($dif < 60) { 
      if ($dif < 2) { 
       return "1 second ago"; 
      } 

      return $dif." seconds ago"; 
     } 

     if ($dif < 3600) { 
      if (floor($dif/60) < 2) { 
       return "A minute ago"; 
      } 

      return floor($dif/60)." minutes ago"; 
     } 

     if (date("d n Y", $timeBase) == date("d n Y", $time)) { 
      return "Today, ".date("g:i A", $time); 
     } 

     if (date("n Y", $timeBase) == date("n Y", $time) && date("d", $timeBase) - date("d", $time) == 1) { 
      return "Yesterday, ".date("g:i A", $time); 
     } 

     if (date("Y", $time) == date("Y", time())) { 
      return date("F, jS g:i A", $time); 
     } 
    } else { 
     $dif = $time - $timeBase; 

     if ($dif < 60) { 
      if ($dif < 2) { 
       return "1 second"; 
      } 

      return $dif." seconds"; 
     } 

     if ($dif < 3600) { 
      if (floor($dif/60) < 2) { 
       return "Less than a minute"; 
      } 

      return floor($dif/60)." minutes"; 
     } 

     if (date("d n Y", ($timeBase + 86400)) == date("d n Y", ($time))) { 
      return "Tomorrow, at ".date("g:i A", $time); 
     } 
    } 

    return date("F, jS g:i A Y", $time); 
} 
5

我用類似這樣的一個小功能,可能不是最好的,並且不應對未來的時間,但它的罰款,如果你想要的是過去。

function ago($when) { 
     $diff = date("U") - $when; 

     // Days 
     $day = floor($diff/86400); 
     $diff = $diff - ($day * 86400); 

     // Hours 
     $hrs = floor($diff/3600); 
     $diff = $diff - ($hrs * 3600); 

     // Mins 
     $min = floor($diff/60); 
     $diff = $diff - ($min * 60); 

     // Secs 
     $sec = $diff; 

     // Return how long ago this was. eg: 3d 17h 4m 18s ago 
     // Skips left fields if they aren't necessary, eg. 16h 0m 27s ago/10m 7s ago 
     $str = sprintf("%s%s%s%s", 
       $day != 0 ? $day."d " : "", 
       ($day != 0 || $hrs != 0) ? $hrs."h " : "", 
       ($day != 0 || $hrs != 0 || $min != 0) ? $min."m " : "", 
       $sec."s ago" 
     ); 

     return $str; 
} 

將一個unix時間戳傳遞給它(你的時間在過去),它會說明它是多久以前。如果您希望返回值略有不同,或者可能希望在其中添加數月或數年,則可輕鬆修改。

+0

這是一個非常好的功能。 – nn2 2011-03-23 19:54:15

+0

非常感謝! – 2014-03-05 08:45:02