Skip to content

Instantly share code, notes, and snippets.

@binjoo
Last active February 14, 2024 04:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binjoo/3926392 to your computer and use it in GitHub Desktop.
Save binjoo/3926392 to your computer and use it in GitHub Desktop.
PHP:TYPECHO:上下篇、微博时间格式化显示
/**
* 显示下一篇
*
* @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