为了实现类似于“刚刚;1小时前;昨天 几点几分;前天 几点几分;年月日 几点几分”的个性化日期效果,你需要按照以下步骤进行操作:

  1. 找到 ExtLabelController.php 文件
  2. 添加新的方法 transtime
  3. 在 run 方法中调用 transtime 方法
  4. 在模板页面中使用该标签

具体步骤

1. 找到 ExtLabelController.php 文件

  1. 打开 ExtLabelController.php 文件
    • 路径:\apps\home\controller\ExtLabelController.php

2. 添加新的方法 transtime

  1. 找到 private function test() 方法

    • 在 test 方法下面添加新的方法 transtime

    示例代码:

    // 转换日期
    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_diff = time() - $time;
                    if ($time_diff < 60) {
                        $str = '刚刚';
                    } elseif ($time_diff < 60 * 60) {
                        $min = floor($time_diff / 60);
                        $str = $min . '分钟前';
                    } elseif ($time_diff < 60 * 60 * 24) {
                        $h = floor($time_diff / (60 * 60));
                        $str = $h . '小时前 ' . $htime;
                    } elseif ($time_diff < 60 * 60 * 24 * 3) {
                        $d = floor($time_diff / (60 * 60 * 24));
                        if ($d == 1) {
                            $str = '昨天 ' . $rtime;
                        } else {
                            $str = '前天 ' . $rtime;
                        }
                    } else {
                        $str = $otime;
                    }
                    return $str;
                },
                $this->content
            );
        }
    }

3. 在 run 方法中调用 transtime 方法

  1. 找到 public function run($content) 方法

    • 在 run 方法中调用 transtime 方法。

    示例代码:

    /* 必备启动函数 */
    public function run($content)
    {
        // 接收数据
        $this->content = $content;
    
        // 执行个人自定义标签函数
        $this->test();
    
        // 转换日期
        $this->transtime();
    
        // 返回数据
        return $this->content;
    }

4. 在模板页面中使用该标签

  1. 在文章内容里添加

    html
     
    {transtime({content:date})}
  2. 在文章列表里添加

    html
     
    {pboot:list}
    {transtime([list:date])}
    {/pboot:list}

示例代码

ExtLabelController.php

// 转换日期
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_diff = time() - $time;
                if ($time_diff < 60) {
                    $str = '刚刚';
                } elseif ($time_diff < 60 * 60) {
                    $min = floor($time_diff / 60);
                    $str = $min . '分钟前';
                } elseif ($time_diff < 60 * 60 * 24) {
                    $h = floor($time_diff / (60 * 60));
                    $str = $h . '小时前 ' . $htime;
                } elseif ($time_diff < 60 * 60 * 24 * 3) {
                    $d = floor($time_diff / (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;
}

模板页面

文章内容里添加

html
 
{transtime({content:date})}

文章列表里添加

html
 
{pboot:list}
{transtime([list:date])}
{/pboot:list}

注意事项

  1. 备份文件

    • 在修改任何文件之前,请确保先备份原始文件。
  2. 测试效果

    • 修改后,在前台页面测试是否已实现预期的日期显示效果。
  3. 其他配置

    • 确保其他配置项没有冲突或影响。

总结

通过以上步骤,你可以实现类似于“刚刚;1小时前;昨天 几点几分;前天 几点几分;年月日 几点几分”的个性化日期效果。这样可以提高用户体验,使日期显示更加人性化。希望这些步骤能帮助你顺利完成设置。