dede系统的时间的默认显示格式是“年-月-日”,不过我们有时候想在时间的显示格式上做优化的话就需要格外调整下。例如,根据文章的发布时间显示为“XX分钟前”、“XX小时前”、“XX天前”等,动态变化显示。下直接上干货:
算法1:
[field:pubdate function="tranTime(@me)" /]
2、修改“include/extend.func.php”文件,添加以下函数:
/*文章发布少间前*/ function tranTime($time) { $rtime = date("m-d H:i",$time); $htime = date("H:i",$time); $time = time() - $time; if ($time < 60) { $str = '刚刚'; } elseif ($time < 60 * 60) { $min = floor($time/60); $str = $min.'钟前'; } elseif ($time < 60 * 60 * 24) { $h = floor($time/(60*60)); $str = $h.'h前 '; } //近三天内 elseif ($time < 60 * 60 * 24 * 3) { $d = floor($time/(60*60*24)); if($d==1) $str = '昨天 '; else $str = '前天 '; } elseif ($time < 60 * 60 * 24 * 30) { $d = floor($time/(60*60*24)); if($d<7) $str =$d.'天前 '; elseif($d<14) $str = '1周前 '; elseif($d<21) $str = '2周前 '; elseif($d<28) $str = '3周前 '; else $str = '4周前 '; } elseif ($time < 60 * 60 * 24 * 180) { $d = floor($time/(60*60*24)); if($d<60) $str ='1月前 '; elseif($d<90) $str = '2月前 '; elseif($d<120) $str = '3月前 '; elseif($d<150) $str = '4月前 '; else $str = '5月前 '; } else { $str = date("m-d",$time);; } return $str; }
修改完成以后就可以在对应的时间的显示位置显示对应的样式,对于有一定基础的伙计,可以根据函数调整显示算法。在这里就不多做赘述。如果还有什么疑问,欢迎下方留言,我看到后会及时回复。
算法2:
if(!function_exists('tranTime')) { function tranTime($time) { $rtime = date("m-d H:i",$time); $htime = date("H:i",$time); $etime = time() - $time; if ($etime < 1) return '刚刚'; $interval = array ( 12 * 30 * 24 * 60 * 60 => ' 年 前', 30 * 24 * 60 * 60 => ' 个 月 前', 7 * 24 * 60 * 60 => ' 周 前', 24 * 60 * 60 => ' 天 前', 60 * 60 => ' 小 时 前', 60 => ' 分 钟 前', 1 => ' 秒 前' ); foreach($interval as $secs => $str) { $d = $etime / $secs; if($d >= 1) { $r = round($d); return $r . $str; } }; } }
[field:pubdate function="tranTime(@me)"/]
{dede:field.pubdate function="tranTime(@me)"/}
如果时间格式是 2018-10-10 这种正常时间,那要这样写调用标签
[field:pubdate function="tranTime(GetMkTime(@me))"/] {dede:field.pubdate function="tranTime(GetMkTime(@me))"/};