将文章日期时间转换为刚刚、几分钟、几小时前的个性化日期效果。
1,找到\apps\home\controller\ExtLabelController.php,添加代码,修改大约在35行,在private function test()函数结束下方添加新函数:
//转换日期
private function transtime(){
$pattern = '/\{transtime\s?\(([^\}]+)\)\}/';
if (preg_match($pattern, $this->content, $matches)) {
$this->content = preg_replace_callback(
$pattern,
function($matches){
$time = strtotime($matches[1]);
$otime = date("Y-m-d H:i",$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.'小时前 '.$htime;
}elseif ($time < 60 * 60 * 24 * 3){
$d = floor($time/(60*60*24));
if($d==1)
$str = '昨天 '.$rtime;
else
$str = '前天 '.$rtime;
}else{
$str = $otime;
}
return $str;
},
$this->content);
}
}
添加执行函数:
/* 必备启动函数 */
public function run($content)
{
// 接收数据
$this->content = $content;
// 执行个人自定义标签函数
$this->test();
//转换日期
$this->transtime();
// 返回数据
return $this->content;
}
最后在模板页面里,调用代码:
//在文章内容里添加
{transtime({content:date})}
//在文章列表里添加
{pboot:list}
{transtime([list:date])}
{/pboot:list}
发表评论