将时间以距今多久的形式表示,PHP,js双版本_PHP教程
程序员文章站
2024-04-02 20:14:16
...
废话不多说直接上代码。
PHP FOR Smarty
* Name: time_ago
* Purpose: 将时间戳专为距当前时间的表现形式
* 1分钟内按秒
* 1小时内按分钟显示
* 1天内按时分显示
* 3天内以昨天,前天显示
* 超过3天显示具体日期
*
* @author Peter Pan
* @param int $time input int
*/
function smarty_modifier_time_ago($time) {
$time_deff = time() - $time;
$retrun = '';
if ($time_deff >= 259200) {
$retrun = date('Y-m-d H:i', $time);
} else if ($time_deff >= 172800) {
$retrun = "前天 " . date('H:i', $time);
} else if ($time_deff >= 86400) {
$retrun = "昨天" . date('H:i', $time);
} else if ($time_deff >= 3600) {
$hour = intval($time_deff / 3600);
$minute = intval(($time_deff % 3600) / 60);
$retrun = $hour . '小时';
if ($minute > 0) {
$retrun .= $minute . '分钟';
}
$retrun .= '前';
} else if ($time_deff >= 60) {
$minute = intval($time_deff / 60);
$second = $time_deff % 60;
$retrun = $minute . '分';
if ($second > 0) {
$retrun .= $second . '秒';
}
$retrun .= '前';
}else{
$retrun = $time_deff.'秒前';
}
return $retrun;
}
Javascript
JS比较复杂一点,分为3个函数实现
/**
* 字符串填充
* @param string str 要进行填充的字符串
* @param int len 目标字符串长度
* @param str chr 用于填充的字符 默认为空格
* @param str dir 填充位置 left|right|both 默认为right
*/
function strPad(str, len, chr, dir){
str = str.toString();
len = (typeof len == 'number') ? len : 0;
chr = (typeof chr == 'string') ? chr : ' ';
dir = (/left|right|both/i).test(dir) ? dir : 'right';
var repeat = function(c, l) {
var repeat = '';
while (repeat.length repeat += c;
}
return repeat.substr(0, l);
}
var diff = len - str.length;
if (diff > 0) {
switch (dir) {
case 'left':
str = '' + repeat(chr, diff) + str;
break;
case 'both':
var half = repeat(chr, Math.ceil(diff / 2));
str = (half + str + half).substr(1, len);
break;
default:
str = '' + str + repeat(chr, diff);
}
}
return str;
}
/**
* 格式化日期
* 类似php Date函数,传入Unix 时间戳(秒级)返回指定格式
* 格式(不区分大小写):
* y 表示4位年份
* m 表示2位月份
* d 表示2位日
* h 表示2位时
* i 表示2位分
* s 表示2位秒
*/
function formatDate(format,timestamp) {
var date = new Date(parseInt(timestamp) * 1000);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
month = strPad(month,2,'0','left');
day = strPad(day,2,'0','left');
hour = strPad(hour,2,'0','left');
minute = strPad(minute,2,'0','left');
second = strPad(second,2,'0','left');
format = format.replace(/y/gi,year);
format = format.replace(/m/gi,month);
format = format.replace(/d/gi,day);
format = format.replace(/h/gi,hour);
format = format.replace(/i/gi,minute);
format = format.replace(/s/gi,second);
return format;
}
function timeAgo(time) {
var nowTime = Date.parse(new Date()) / 1000;
var time_deff = nowTime - time;
retrun = '';
if (time_deff >= 259200) {
retrun = formatDate('Y-m-d H:i', time);
} else if (time_deff >= 172800) {
retrun = "前天 " + formatDate('H:i', time);
} else if (time_deff >= 86400) {
retrun = "昨天" + formatDate('H:i', time);
} else if (time_deff >= 3600) {
hour = parseInt(time_deff / 3600);
minute = parseInt((time_deff % 3600) / 60);
retrun = hour + '小时';
if (minute > 0) {
retrun += minute + '分钟';
}
retrun += '前';
} else if (time_deff >= 60) {
minute = parseInt($time_deff / 60);
second = time_deff % 60;
retrun = minute + '分';
if (second > 0) {
retrun += second + '秒';
}
retrun += '前';
}else{
retrun = time_deff +'秒前';
}
return retrun;
}
PHP FOR Smarty
复制代码 代码如下:
* Name: time_ago
* Purpose: 将时间戳专为距当前时间的表现形式
* 1分钟内按秒
* 1小时内按分钟显示
* 1天内按时分显示
* 3天内以昨天,前天显示
* 超过3天显示具体日期
*
* @author Peter Pan
* @param int $time input int
*/
function smarty_modifier_time_ago($time) {
$time_deff = time() - $time;
$retrun = '';
if ($time_deff >= 259200) {
$retrun = date('Y-m-d H:i', $time);
} else if ($time_deff >= 172800) {
$retrun = "前天 " . date('H:i', $time);
} else if ($time_deff >= 86400) {
$retrun = "昨天" . date('H:i', $time);
} else if ($time_deff >= 3600) {
$hour = intval($time_deff / 3600);
$minute = intval(($time_deff % 3600) / 60);
$retrun = $hour . '小时';
if ($minute > 0) {
$retrun .= $minute . '分钟';
}
$retrun .= '前';
} else if ($time_deff >= 60) {
$minute = intval($time_deff / 60);
$second = $time_deff % 60;
$retrun = $minute . '分';
if ($second > 0) {
$retrun .= $second . '秒';
}
$retrun .= '前';
}else{
$retrun = $time_deff.'秒前';
}
return $retrun;
}
Javascript
JS比较复杂一点,分为3个函数实现
复制代码 代码如下:
/**
* 字符串填充
* @param string str 要进行填充的字符串
* @param int len 目标字符串长度
* @param str chr 用于填充的字符 默认为空格
* @param str dir 填充位置 left|right|both 默认为right
*/
function strPad(str, len, chr, dir){
str = str.toString();
len = (typeof len == 'number') ? len : 0;
chr = (typeof chr == 'string') ? chr : ' ';
dir = (/left|right|both/i).test(dir) ? dir : 'right';
var repeat = function(c, l) {
var repeat = '';
while (repeat.length repeat += c;
}
return repeat.substr(0, l);
}
var diff = len - str.length;
if (diff > 0) {
switch (dir) {
case 'left':
str = '' + repeat(chr, diff) + str;
break;
case 'both':
var half = repeat(chr, Math.ceil(diff / 2));
str = (half + str + half).substr(1, len);
break;
default:
str = '' + str + repeat(chr, diff);
}
}
return str;
}
/**
* 格式化日期
* 类似php Date函数,传入Unix 时间戳(秒级)返回指定格式
* 格式(不区分大小写):
* y 表示4位年份
* m 表示2位月份
* d 表示2位日
* h 表示2位时
* i 表示2位分
* s 表示2位秒
*/
function formatDate(format,timestamp) {
var date = new Date(parseInt(timestamp) * 1000);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
month = strPad(month,2,'0','left');
day = strPad(day,2,'0','left');
hour = strPad(hour,2,'0','left');
minute = strPad(minute,2,'0','left');
second = strPad(second,2,'0','left');
format = format.replace(/y/gi,year);
format = format.replace(/m/gi,month);
format = format.replace(/d/gi,day);
format = format.replace(/h/gi,hour);
format = format.replace(/i/gi,minute);
format = format.replace(/s/gi,second);
return format;
}
function timeAgo(time) {
var nowTime = Date.parse(new Date()) / 1000;
var time_deff = nowTime - time;
retrun = '';
if (time_deff >= 259200) {
retrun = formatDate('Y-m-d H:i', time);
} else if (time_deff >= 172800) {
retrun = "前天 " + formatDate('H:i', time);
} else if (time_deff >= 86400) {
retrun = "昨天" + formatDate('H:i', time);
} else if (time_deff >= 3600) {
hour = parseInt(time_deff / 3600);
minute = parseInt((time_deff % 3600) / 60);
retrun = hour + '小时';
if (minute > 0) {
retrun += minute + '分钟';
}
retrun += '前';
} else if (time_deff >= 60) {
minute = parseInt($time_deff / 60);
second = time_deff % 60;
retrun = minute + '分';
if (second > 0) {
retrun += second + '秒';
}
retrun += '前';
}else{
retrun = time_deff +'秒前';
}
return retrun;
}
上一篇: PHP输出,为什么两者不一样
下一篇: 同时提取多条新闻中的文本一例_PHP教程