当前位置: 首页 > 织梦教程 > 织梦显示发布时间为多少时间前的实现方法

织梦显示发布时间为多少时间前的实现方法

365ymw / 2020-03-20
[] [] []

 织梦的文章发布时间调用格式一般分为一下这些:

01 [field:pubdate function="MyDate('Y-m-d',@me)"/]
02 2013-12-17
03 [field:pubdate function=MyDate('m-d',@me)/]
04 12-17
05 [field:pubdate function=MyDate('y-m-d',@me)/]
06 13-12-17
07 [field:pubdate function='strftime("%y-%m-%d %H:%M:%S",@me)'/]
08 13-12-17 10:35:21
09 [field:pubdate function='strftime("%m-%d %H:%M:%S",@me)'/]
10 12-17 10:35:21
11 [field:pubdate function='strftime("%y年%m月%d日",@me)'/]
12 13年12月17日
13 [field:pubdate function='strftime("%Y年%m月%d日 %H点%M分%S秒",@me)'/]
14 13年12月17日 12点12分30秒

 

有的时候,我们想做到不直接显示具体时间,而是以几天前、几月前这样的时间间隔方式来显示, 更利于让访客直观地看到文章的发布时段,以上的调用方法则不适用了。

教大家如何实现显示发布时间为多少时间前,主要有两种方法。

 

一、模板直接写PHP语句

01 [field:pubdate runphp='yes']
02 $today = Floor(time()/(3600 * 24));
03 $senday= Floor(@me/(3600 * 24));
04 $updays = $today-$senday;
05 if($updays >= 30 && $updays < 60) @me="1个月前";
06 elseif($updays >= 60 && $updays < 90) @me="2个月前";
07 elseif($updays >= 90 && $updays < 120) @me="3个月前";
08 elseif($updays >= 120 && $updays < 150) @me="4个月前";
09 elseif($updays >= 150 && $updays < 180) @me="5个月前";
10 elseif($updays >= 180 && $updays < 210) @me="6个月前";
11 elseif($updays >= 210 && $updays < 240) @me="7个月前";
12 elseif($updays >= 240 && $updays < 270) @me="8个月前";
13 elseif($updays >= 270 && $updays < 300) @me="9个月前";
14 elseif($updays > 300 && $updays < 330) @me="10个月前";
15 elseif($updays > 330 && $updays < 360) @me="11个月前";
16 elseif($updays >= 360) @me="一年前";
17 elseif($updays==0) @me = "今日";
18 else @me = $updays."天前";
19 [/field:pubdate]

 

二、自定义函数

在include/extend.func.php文件中加入以下代码:


 

01 //文章发布多少时间前
02       function tranTime($time) {
03       $today = Floor(time()/(3600 * 24));
04       $senday= Floor($time/(3600 * 24));
05       $updays = $today-$senday;
06       if($updays==0)
07       $str = '今天';
08       elseif ($updays >=1 && $updays < 31) {
09               $str = $updays.'天前 ';
10       }
11       elseif ($updays >= 31&& $updays < 365) {
12               $m = floor($updays / 31);
13               $str = $m.'月前 ';
14       }
15           elseif ($updays >= 31&& $updays < 365) {
16               $y = floor($updays / (31* 365));
17               $str = $y.'年前 ';
18       }
19       else {
20               $str = $rtime;
21       }
22       return $str;
23                                                     }

如果要显示几分钟和几小时,则自行加入判断函数

        

1 elseif ($updays >0 && $updays < 1 ) {
2  
3         $h = floor($updays * 24);
4  
5         $str = $h.'小时前 ';
6  
7         }

 

调用方法

列表页:[field:pubdate function="tranTime(@me)" /]

内容页:{dede:field.pubdate function="tranTime(@me)"/}

 

以上两种方法都可以实现调用,第一种方法是直接把php语法写入模版中,会显得文件比较冗余,建议使用第二种自定函数的方式,简单明了。

本文链接:http://www.365ymw.com/article-1472.html 转载请保留出处,谢谢!

中查找“织梦显示发布时间为多少时间前的实现方法”更多相关内容

用户评论

暂时还没有任何用户评论
用户名: 匿名用户
E-mail:
评价等级:
评论内容:
验证码: captcha

浏览历史

免责条款 | 隐私保护 | 联系我们 | 公司简介 | 报价单 | 积分商城 | 留言板
Copyright © 2010-2024 365ymw.com All Rights Reserved. 365源码商城
免责声明:部分图片素材及板块内容来自网络,如有侵犯您的权益,请联系我们,站长会立即处理!
备案号:鄂ICP备2024039974号