Skip to content

Instantly share code, notes, and snippets.

@safecat
Created September 2, 2014 06:00
Show Gist options
  • Save safecat/ecf5ea254ef4d15f97f0 to your computer and use it in GitHub Desktop.
Save safecat/ecf5ea254ef4d15f97f0 to your computer and use it in GitHub Desktop.
uetime 个性化日期显示
<?php
/**
* 个性化日期显示
* @static
* @access public
* @param datetime $times 日期
* @return string 返回大致日期
* @example 示例 ueTime('')
* @author http://segmentfault.com/q/1010000000650101
*/
function ueTime($times) {
if ($times == '' || $times == 0) {
return false;
}
//完整时间戳
$strtotime = is_int($times) ? $times : strtotime($times);
$times_day = date('Y-m-d', $strtotime);
$times_day_strtotime = strtotime($times_day);
//今天
$nowdate_str = strtotime(date('Y-m-d'));
//精确的时间间隔(秒)
$interval = time() - $strtotime;
//今天的
if ($times_day_strtotime == $nowdate_str) {
//小于一分钟
if ($interval < 60) {
$pct = sprintf("%d秒前", $interval);
}
//小于1小时
elseif ($interval < 3600) {
$pct = sprintf("%d分钟前", ceil($interval / 60));
} else {
$pct = sprintf("%d小时前", floor($interval / 3600));
}
}
//昨天的
elseif ($times_day_strtotime == strtotime(date('Y-m-d', strtotime('-1 days')))) {
$pct = '昨天' . date('H:i', $strtotime);
}
//前天的
elseif ($times_day_strtotime == strtotime(date('Y-m-d', strtotime('-2 days')))) {
$pct = '前天' . date('H:i', $strtotime);
}
//一个月以内
elseif ($interval < (3600 * 24 * 30)) {
$pct = date('m月d日', $strtotime);
}
//一年以内
elseif ($interval < (3600 * 24 * 365)) {
$pct = date('m月d日', $strtotime);
}
//一年以上
else {
$pct = date('Y年m月d日', $strtotime);
}
return $pct;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment