要实现类似功能,用js就可以,实现方法如下:
一、javascript函数实现:
实例1:
复制代码 代码如下:
//javascript函数:
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
function getdatediff(datetimestamp){
var now = new date().gettime();
var diffvalue = now - datetimestamp;
if(diffvalue //若日期不符则弹出窗口告之
//alert(结束日期不能小于开始日期!);
}
var monthc =diffvalue/month;
var weekc =diffvalue/(7*day);
var dayc =diffvalue/day;
var hourc =diffvalue/hour;
var minc =diffvalue/minute;
if(monthc>=1){
result=发表于 + parseint(monthc) + 个月前;
}
else if(weekc>=1){
result=发表于 + parseint(weekc) + 周前;
}
else if(dayc>=1){
result=发表于+ parseint(dayc) +天前;
}
else if(hourc>=1){
result=发表于+ parseint(hourc) +个小时前;
}
else if(minc>=1){
result=发表于+ parseint(minc) +分钟前;
}else
result=刚刚发表;
return result;
}
若你得到的时间格式不是时间戳,可以使用下面的javascript函数把字符串转换为时间戳, 本函数的功能相当于js版的strtotime:
复制代码 代码如下:
//js函数代码:字符串转换为时间戳
function getdatetimestamp(datestr){
return date.parse(datestr.replace(/-/gi,/));
}
实例2:
复制代码 代码如下:
二、jquery插件实现
html代码:
复制代码 代码如下:
调用代码:
复制代码 代码如下:
jquery(span.timeago).timeago();
插件源码:
复制代码 代码如下:
(function (factory) {
if (typeof define === 'function' && define.amd) {
// amd. register as an anonymous module.
define(['jquery'], factory);
} else {
// browser globals
factory(jquery);
}
}(function ($) {
$.timeago = function(timestamp) {
if (timestamp instanceof date) {
return inwords(timestamp);
} else if (typeof timestamp === string) {
return inwords($.timeago.parse(timestamp));
} else if (typeof timestamp === number) {
return inwords(new date(timestamp));
} else {
return inwords($.timeago.datetime(timestamp));
}
};
var $t = $.timeago;
$.extend($.timeago, {
settings: {
refreshmillis: 60000,
allowfuture: false,
localetitle: false,
cutoff: 0,
strings: {
prefixago: null,
prefixfromnow: null,
suffixago: 前,
suffixfromnow: from now,
seconds: 1分钟,
minute: 1分钟,
minutes: %d分钟,
hour: 1小时,
hours: %d小时,
day: 1天,
days: %d天,
month: 1月,
months: %d月,
year: 1年,
years: %d年,
wordseparator: ,
numbers: []
}
},
inwords: function(distancemillis) {
var $l = this.settings.strings;
var prefix = $l.prefixago;
var suffix = $l.suffixago;
if (this.settings.allowfuture) {
if (distancemillis prefix = $l.prefixfromnow;
suffix = $l.suffixfromnow;
}
}
var seconds = math.abs(distancemillis) / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var years = days / 365;
function substitute(stringorfunction, number) {
var string = $.isfunction(stringorfunction) ? stringorfunction(number, distancemillis) : stringorfunction;
var value = ($l.numbers && $l.numbers[number]) || number;
return string.replace(/%d/i, value);
}
var words = seconds seconds minutes minutes hours hours days days days years substitute($l.years, math.round(years));
var separator = $l.wordseparator || ;
if ($l.wordseparator === undefined) { separator = ; }
return $.trim([prefix, words, suffix].join(separator));
},
parse: function(iso8601) {
var s = $.trim(iso8601);
s = s.replace(/\.\d+/,); // remove milliseconds
s = s.replace(/-/,/).replace(/-/,/);
s = s.replace(/t/, ).replace(/z/, utc);
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/, $1$2); // -04:00 -> -0400
return new date(s);
},
datetime: function(elem) {
var iso8601 = $t.istime(elem) ? $(elem).attr(datetime) : $(elem).attr(title);
return $t.parse(iso8601);
},
istime: function(elem) {
// jquery's `is()` doesn't play well with html5 in ie
return $(elem).get(0).tagname.tolowercase() === time; // $(elem).is(time);
}
});
// functions that can be called via $(el).timeago('action')
// init is default when no action is given
// functions are called with context of a single element
var functions = {
init: function(){
var refresh_el = $.proxy(refresh, this);
refresh_el();
var $s = $t.settings;
if ($s.refreshmillis > 0) {
setinterval(refresh_el, $s.refreshmillis);
}
},
update: function(time){
$(this).data('timeago', { datetime: $t.parse(time) });
refresh.apply(this);
},
updatefromdom: function(){
$(this).data('timeago', { datetime: $t.parse( $t.istime(this) ? $(this).attr(datetime) : $(this).attr(title) ) });
refresh.apply(this);
}
};
$.fn.timeago = function(action, options) {
var fn = action ? functions[action] : functions.init;
if(!fn){
throw new error(unknown function name '+ action +' for timeago);
}
// each over objects here and call the requested function
this.each(function(){
fn.call(this, options);
});
return this;
};
function refresh() {
var data = preparedata(this);
var $s = $t.settings;
if (!isnan(data.datetime)) {
if ( $s.cutoff == 0 || distance(data.datetime) $(this).text(inwords(data.datetime));
}
}
return this;
}
function preparedata(element) {
element = $(element);
if (!element.data(timeago)) {
element.data(timeago, { datetime: $t.datetime(element) });
var text = $.trim(element.text());
if ($t.settings.localetitle) {
element.attr(title, element.data('timeago').datetime.tolocalestring());
} else if (text.length > 0 && !($t.istime(element) && element.attr(title))) {
element.attr(title, text);
}
}
return element.data(timeago);
}
function inwords(date) {
return $t.inwords(distance(date));
}
function distance(date) {
return (new date().gettime() - date.gettime());
}
// fix for ie6 suckage
document.createelement(abbr);
document.createelement(time);
}));