織夢的文章發布時間調用格式一般分為一下這些:
[field:pubdate function="MyDate('Y-m-d',@me)"/] 2013-12-17 [field:pubdate function=MyDate('m-d',@me)/] 12-17 [field:pubdate function=MyDate('y-m-d',@me)/] 13-12-17 [field:pubdate function='strftime("%y-%m-%d %H:%M:%S",@me)'/] 13-12-17 10:35:21 [field:pubdate function='strftime("%m-%d %H:%M:%S",@me)'/] 12-17 10:35:21 [field:pubdate function='strftime("%y年%m月%d日",@me)'/] 13年12月17日 [field:pubdate function='strftime("%Y年%m月%d日 %H點%M分%S秒",@me)'/] 13年12月17日 12點12分30秒 |
有的時候,我們想做到不直接顯示具體時間,而是以幾天前、幾月前這樣的時間間隔方式來顯示, 更利于讓訪客直觀地看到文章的發布時段,以上的調用方法則不適用了。
教大家如何實現顯示發布時間為多少時間前,主要有兩種方法。
一、模板直接寫PHP語句
[field:pubdate runphp='yes'] $today = Floor(time()/(3600 * 24)); $senday= Floor(@me/(3600 * 24)); $updays = $today-$senday; if($updays >= 30 && $updays < 60) @me="1個月前"; elseif($updays >= 60 && $updays < 90) @me="2個月前"; elseif($updays >= 90 && $updays < 120) @me="3個月前"; elseif($updays >= 120 && $updays < 150) @me="4個月前"; elseif($updays >= 150 && $updays < 180) @me="5個月前"; elseif($updays >= 180 && $updays < 210) @me="6個月前"; elseif($updays >= 210 && $updays < 240) @me="7個月前"; elseif($updays >= 240 && $updays < 270) @me="8個月前"; elseif($updays >= 270 && $updays < 300) @me="9個月前"; elseif($updays > 300 && $updays < 330) @me="10個月前"; elseif($updays > 330 && $updays < 360) @me="11個月前"; elseif($updays >= 360) @me="一年前"; elseif($updays==0) @me = "今日"; else @me = $updays."天前"; [/field:pubdate] |
二、自定義函數
在include/extend.func.php文件中加入以下代碼:
//文章發布多少時間前 function tranTime($time) { $today = Floor(time()/(3600 * 24)); $senday= Floor($time/(3600 * 24)); $updays = $today-$senday; if($updays==0) $str = '今天'; elseif ($updays >=1 && $updays < 31) { $str = $updays.'天前 '; } elseif ($updays >= 31&& $updays < 365) { $m = floor($updays / 31); $str = $m.'月前 '; } elseif ($updays >= 31&& $updays < 365) { $y = floor($updays / (31* 365)); $str = $y.'年前 '; } else { $str = $rtime; } return $str; } |
如果要顯示幾分鐘和幾小時,則自行加入判斷函數
elseif ($updays >0 && $updays < 1 ) { $h = floor($updays * 24); $str = $h.'小時前 '; } |
調用方法
列表頁:[field:pubdate function="tranTime(@me)" /]
內容頁:{dede:field.pubdate function="tranTime(@me)"/}
以上兩種方法都可以實現調用,第一種方法是直接把php語法寫入模版中,會顯得文件比較冗余,建議使用第二種自定函數的方式,簡單明了。