[教程] Typecho 类似微博上几分钟前、几天前

查看: 522|回复: 0
crll 发表于 2023-2-25 21:15:03
这样可以实现,在functions.php里添加如下代码:
  1. function getDayAgo($date){
  2. $d = new Typecho_Date(Typecho_Date::gmtTime());
  3. $now = $d->format('Y-m-d H:i:s');
  4. $t = strtotime($now) - strtotime($date);
  5. if($t < 60){
  6. return $t . '秒前';
  7. }
  8. if($t < 3600){
  9. return floor($t / 60) .  '分钟前';
  10. }
  11. if($t < 86400){
  12. return floor($t / 3670) . '小时前';
  13. }
  14. if($t < 604800){
  15. return floor($t / 86400) . '天前';
  16. }
  17. if($t < 2419200){
  18. return floor($t / 604800) .  '周前';
  19. }
  20. if($t < 31536000 ){
  21. return floor($t / 2592000 ).'月前';
  22. }
  23. return floor($t / 31536000 ).'年前';
  24. }
复制代码


调用方法不变,比如在post.php调用文章发布时间:
  1. <?php $this->date('Y-m-d H:i'); ?>
复制代码
博主论坛 bzlt.net
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于本站联系我们FAQ友情链接免责声明生存法则

Build with for "make" Copyright © 2020-2022. Powered by Discuz! GMT+8, 2024-4-27 08:35

快速回复 返回顶部 返回列表