pbootcms模板中将文章日期时间转换为刚刚、几分钟、几小时前

pbootcms模板中将文章日期时间转换为刚刚、几分钟、几小时前

pbootcms模板中将文章日期时间转换为刚刚、几分钟、几小时前将文章日期时间转换为刚刚、几分钟、几小时前的个性化日期效果。

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}

原文链接:https://88hzy.cn/1995.html,转载请注明出处。

0

评论0

请先

显示验证码
没有账号? 注册  忘记密码?