下面是php实现的几种方式 可以自己选用
//把时间戳转换为几分钟或几小时前或几天前
function wordTime($time) {
$time = (int) substr($time, 0, 10);
$int = time() - $time;
$str = '';
if ($int <= 30){
$str = sprintf('刚刚', $int);
}elseif ($int < 60){
$str = sprintf('%d秒前', $int);
}elseif ($int < 3600){
$str = sprintf('%d分钟前', floor($int / 60));
}elseif ($int < 86400){
$str = sprintf('%d小时前', floor($int / 3600));
}elseif ($int < 2592000){
$str = sprintf('%d天前', floor($int / 86400));
}else{
$str = date('Y-m-d H:i:s', $time);
}
return $str;
}
function mdate($time = NULL) {
$text = '';
$time = $time === NULL || $time > time() ? time() : intval($time);
$t = time() - $time; //时间差 (秒)
$y = date('Y', $time)-date('Y', time());//是否跨年
switch($t){
case $t == 0:
$text = '刚刚';
break;
case $t < 60:
$text = $t . '秒前'; // 一分钟内
break;
case $t < 60 * 60:
$text = floor($t / 60) . '分钟前'; //一小时内
break;
case $t < 60 * 60 * 24:
$text = floor($t / (60 * 60)) . '小时前'; // 一天内
break;
case $t < 60 * 60 * 24 * 3:
$text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
break;
case $t < 60 * 60 * 24 * 30:
$text = date('m月d日 H:i', $time); //一个月内
break;
case $t < 60 * 60 * 24 * 365&&$y==0:
$text = date('m月d日', $time); //一年内
break;
default:
$text = date('Y年m月d日', $time); //一年以前
break;
}
return $text;
}
希望对于大家有所帮助!!!!