2012-07-14 55 views

回答

3
<?php 
function get_timeago($ptime) 
{ 
    $etime = time() - $ptime; 

    if($etime < 1) 
    { 
     return 'less than 1 second ago'; 
    } 

    $a = array(12 * 30 * 24 * 60 * 60 => 'year', 
       30 * 24 * 60 * 60  => 'month', 
       24 * 60 * 60   => 'day', 
       60 * 60    => 'hour', 
       60     => 'minute', 
       1     => 'second' 
    ); 

    foreach($a as $secs => $str) 
    { 
     $d = $etime/$secs; 

     if($d >= 1) 
     { 
      $r = round($d); 
      return 'about ' . $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago'; 
     } 
    } 
} 
?> 


Usage: 

    <?php 


    echo get_timeago($timestamp); 

    ?> 

也可以嘗試strtotime()

+0

感謝您的幫助,而不是僅僅說了一個複製哇。 – dragonfire1119 2012-07-14 07:09:03