格式化友好时间格式
Posted *紫色薰衣草*
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了格式化友好时间格式相关的知识,希望对你有一定的参考价值。
<td height="40" title="<{$rs.regtime|date=‘Y-m-d H:i:s‘,###}>"><{$rs.regtime|mdate}></td>
/**
* 格式化友好时间格式
* @param unknown $time
* @return string
*/
function mdate($time = NULL) {
$text = ‘‘;
$time = $time === NULL || $time > time() ? time() : intval($time);
$t = time() - $time; //时间差 (秒)
if ($t == 0)
$text = ‘刚刚‘;
elseif ($t < 60)
$text = $t . ‘秒前‘; // 一分钟内
elseif ($t < 60 * 60)
$text = floor($t / 60) . ‘分钟前‘; //一小时内
elseif ($t < 60 * 60 * 24)
$text = floor($t / (60 * 60)) . ‘小时前‘; // 一天内
elseif ($t < 60 * 60 * 24 * 3)
$text = floor($time/(60*60*24)) ==1 ?‘昨天 ‘ . date(‘H:i‘, $t) : ‘前天 ‘ . date(‘H:i‘, $time) ; //昨天和前天
elseif ($t < 60 * 60 * 24 * 30)
$text = date(‘m月d日 H:i‘, $time); //一个月内
elseif ($t < 60 * 60 * 24 * 365)
$text = date(‘m月d日‘, $time); //一年内
else
$text = date(‘Y年m月d日‘, $time); //一年以前
return $text;
}
以上是关于格式化友好时间格式的主要内容,如果未能解决你的问题,请参考以下文章
.NET Core 中使用 Humanizer 显示友好时间格式
js 显示友好的时间格式刚刚几分钟前几小时几天前几周前几月前等等 时间格式化(工具类)
如何使用包含友好日期和月份的 DateFormatter 格式化日期