zblogPHP如何实现X小时前的友好时间?
将以下代码添加到主题的 include.php 文件中:
- functionTimeAgo($ptime){
- $ptime=strtotime($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($intervalas$secs=>$str){
- $d=$etime/$secs;
- if($d>=1){
- $r=round($d);
- return$r.$str;
- }
- };
- }
然后在需要显示文章时间的地方添加以下代码即可:
- {TimeAgo($article.Time())}
内容整理自:Z-Blog Wiki - https://wiki.zblogcn.com/doku.php?id=zblogphp:development:theme:tips
