| 这几天没空写代码,也就没啥时间写文字了,分享一个发布文章的时间与当前时间的间隔函数,诸如:“发布于30分钟前”这样的效果,很简单的代码。 调用方法也很简单,timeago('需要与当前时间进行计算的unix时间戳'); 
 function timeago( $ptime ) { $etime = time() - $ptime; if ($etime < 1) return '刚刚';
 $interval = array (
 12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
 30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')',
 7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
 24 * 60 * 60 => '天前',
 60 * 60 => '小时前',
 60 => '分钟前',
 1 => '秒前' ); foreach ($interval as $secs => $str) {
 $d = $etime / $secs;
 if ($d >= 1) {
 $r = round($d);
 return $r . $str;
 }
 }
 }
 
 |