Last active
February 14, 2024 04:45
-
-
Save binjoo/3926392 to your computer and use it in GitHub Desktop.
PHP:TYPECHO:上下篇、微博时间格式化显示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 显示下一篇 | |
* | |
* @access public | |
* @param string $default 如果没有下一篇,显示的默认文字 | |
* @return void | |
*/ | |
function theNext($widget, $default = NULL) | |
{ | |
$db = Typecho_Db::get(); | |
$sql = $db->select()->from('table.contents') | |
->where('table.contents.created > ?', $widget->created) | |
->where('table.contents.status = ?', 'publish') | |
->where('table.contents.type = ?', $widget->type) | |
->where('table.contents.password IS NULL') | |
->order('table.contents.created', Typecho_Db::SORT_ASC) | |
->limit(1); | |
$content = $db->fetchRow($sql); | |
if ($content) { | |
$content = $widget->filter($content); | |
$link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">' . $content['title'] . '</a><span>' . time_tran($content['created']) . '</span>'; | |
echo $link; | |
} else { | |
echo $default; | |
} | |
} | |
/** | |
* 显示上一篇 | |
* | |
* @access public | |
* @param string $default 如果没有下一篇,显示的默认文字 | |
* @return void | |
*/ | |
function thePrev($widget, $default = NULL) | |
{ | |
$db = Typecho_Db::get(); | |
$sql = $db->select()->from('table.contents') | |
->where('table.contents.created < ?', $widget->created) | |
->where('table.contents.status = ?', 'publish') | |
->where('table.contents.type = ?', $widget->type) | |
->where('table.contents.password IS NULL') | |
->order('table.contents.created', Typecho_Db::SORT_DESC) | |
->limit(1); | |
$content = $db->fetchRow($sql); | |
if ($content) { | |
$content = $widget->filter($content); | |
$link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">' . $content['title'] . '</a><span>' . time_tran($content['created']) . '</span>'; | |
echo $link; | |
} else { | |
echo $default; | |
} | |
} | |
/** 微博时间格式化显示 | |
* @param $timestamp,标准时间戳 | |
*/ | |
function time_tran($timestamp){ | |
$now_time = abs(time()-$timestamp); | |
$gmt_offset = Helper::options()->timezone;//获取typecho的时区偏移值 | |
$timestamp += $gmt_offset; | |
$current_time = mktime() + $gmt_offset; | |
$dur = $current_time - $timestamp; | |
if($dur < 0){ | |
return $timestamp; | |
}else if($dur < 60){//一分钟内 | |
return $dur.'秒前'; | |
}else if($dur < 60 * 60){//一小时内 | |
return floor($dur/60).'分钟前'; | |
}else if($dur < 60 * 60 * 24){//一天内 | |
return floor($dur/3600).'小时前'; | |
}else if($dur < 60 * 60 * 24 * 30){//30天内 | |
return floor($dur/86400).'天前'; | |
}else if($dur < 60 * 60 * 24 * 30 * 3){//3个月内 | |
return floor($dur/2592000).'个月前'; | |
}else{ | |
return gmdate('Y-m-d H:i',$timestamp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment