<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_61052"> 将时间转成几天前,几小时等的格式,如: “1天前”,“2个月前”等。 <div class="blockcode"> <div id="code_J89"><ol> <li> <?php <li>function prettyDate($date){</li> <li> $time = strtotime($date);</li> <li> $now = time();</li> <li> $ago = $now - $time;</li> <li> if($ago $when = round($ago);</li> <li> $s = ($when == 1)?"second":"seconds";</li> <li> return "$when $s ago";</li> <li> }elseif($ago $when = round($ago / 60);</li> <li> $m = ($when == 1)?"minute":"minutes";</li> <li> return "$when $m ago";</li> <li> }elseif($ago >= 3600 && $ago $when = round($ago / 60 / 60);</li> <li> $h = ($when == 1)?"hour":"hours";</li> <li> return "$when $h ago";</li> <li> }elseif($ago >= 86400 && $ago $when = round($ago / 60 / 60 / 24);</li> <li> $d = ($when == 1)?"day":"days";</li> <li> return "$when $d ago";</li> <li> }elseif($ago >= 2629743.83 && $ago $when = round($ago / 60 / 60 / 24 / 30.4375);</li> <li> $m = ($when == 1)?"month":"months";</li> <li> return "$when $m ago";</li> <li> }else{</li> <li> $when = round($ago / 60 / 60 / 24 / 365);</li> <li> $y = ($when == 1)?"year":"years";</li> <li> return "$when $y ago";</li> <li> }</li> <li>}</li> <li> <li>echo prettyDate("2012-07-22 12:23:45")."<br>";</li> <li>echo prettyDate("2010-11-12 22:25:45")."<br>";</li> <li>echo prettyDate("2012-01-01 01:00:00")."<br>";</li> <li>echo prettyDate("2001-05-30 00:00:00")."<br>"; </li> </ol></div> <em onclick="copycode($('code_J89'));">复制代码</em> </div> <br> </td></tr></table> <div class="ptg mbm mtn"> 几天, 转成</div> <div id="comment_61052" class="cm"> </div> <div id="post_rate_div_61052"></div> <br><br>