
请问Pbootcms将日期时间转换成"刚刚、几分钟、几小时前"的形式

为了实现类似于“刚刚;1小时前;昨天 几点几分;前天 几点几分;年月日 几点几分”的个性化日期效果,你需要按照以下步骤进行操作:
- 找到
ExtLabelController.php
文件。 - 添加新的方法
transtime
。 - 在
run
方法中调用transtime
方法。 - 在模板页面中使用该标签。
具体步骤
1. 找到 ExtLabelController.php
文件
- 打开
ExtLabelController.php
文件:- 路径:
\apps\home\controller\ExtLabelController.php
- 路径:
2. 添加新的方法 transtime
-
找到
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
方法
-
找到
public function run($content)
方法:- 在
run
方法中调用transtime
方法。
示例代码:
/* 必备启动函数 */ public function run($content) { // 接收数据 $this->content = $content; // 执行个人自定义标签函数 $this->test(); // 转换日期 $this->transtime(); // 返回数据 return $this->content; }
- 在
4. 在模板页面中使用该标签
-
在文章内容里添加:
html{transtime({content:date})}
-
在文章列表里添加:
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小时前;昨天 几点几分;前天 几点几分;年月日 几点几分”的个性化日期效果。这样可以提高用户体验,使日期显示更加人性化。希望这些步骤能帮助你顺利完成设置。
标签:bootstrap日期 pb显示时间 pb时间戳 pb日期控件 pb 日期 时间 函数
更新日期:2025-03-09 00:27:28 来源:网络
下一篇:请问pbootcms常用的13个IF判断语句大全汇总

- 请问重置网站后台密码-云服务器问题
- 请问请问如何修改zblog的数据库连接信息?
- 请问高效管理公司网站,选择合适的网站修改工具
- 请问解决虚拟主机预装网站无法访问的问题
- 请问请问如何修改zblog的数据库连接信息以适应新的主机环境?
- 请问重置网站后台密码的正确途径
- 请问解决网站频繁出现 500 内部服务器错误的方法
- 请问移动端安全 - 正确修改移动端网站密码的方法与技巧
- 请问帝国CMS进阶 - 修改帝国管理网站模板的详细步骤与实用技巧
- 请问网站升级数据迁移之后无法访问
- 如何解决PbootCMS网站后台推送文章到百度时的“site error”错误?
- 网站admin密码忘记了怎么办
- 宝塔面板提示不是安全连接或私密连接,如何解决?
- 迅睿CMS网站用户投稿如何取消验证码
- {pboot:sql sql="语句"}[sql:字段]{/pboot:sql}万能循环标签使用介绍
- 网站提示429 Too Many Requests:用户发送了太多请求怎么办
- PbootCMS上传文件大小限制
- 如何优化网站站长信息 - 提升SEO表现与用户体验的最佳实践
- 易优cms搜索结果页如何统计关键词文章数量
- 网站源码安装后访问首页,页面错乱的处理方法
- 重置网站后台管理员账号密码
- 百度网址安全中心提醒您:该页面可能存在违法信息!如何处理?
- pbootcms模板如何做好防护
- pbootcms如何设置发布内容不自动提取缩略图(1)
- 网站提示422 Unprocessable Entity:请求格式正确但请求无法被处理怎么办
- 升级完后网站提示500错误怎么办
- Pbootcms留言“提交成功”的提示语修改(1)
- 易优cms数据表或视图不存在,请联系技术处理。
- Nginx 启动/停止 / 首页显示 / 安装 / 卸载
- PBOOTCMS中新增并开启手机端模板,以便为用户提供更好的移动设备浏览体验