您好,登錄后才能下訂單哦!
/*
@desc:時間友好顯示函數(shù)
@param stamp 時間綴,10位長度
*/
function timefriend(stamp){
this.stamp = stamp
/*
@desc:主方法,執(zhí)行轉(zhuǎn)換
*/
this.get = function(){
var stamp = this.stamp
if(!stamp){
return '—';
}
var date = new Date(stamp*1000)
var time = Math.round(new Date().getTime()/1000)
diff = time - stamp
if(diff<0){
return '—';
}else if(diff<60){
return diff+'秒前'
}else if(diff<3600){
return Math.floor(diff/60)+'分鐘前'
}else if(diff<86400){
return Math.floor(diff/3600)+'小時前'
}else if(diff<259200){
return Math.floor(diff/86400)+'天前'
}else{
var year = this.parsetime(date.getFullYear())
var month = this.parsetime(date.getMonth())
var day = this.parsetime(date.getDate())
var hour = this.parsetime(date.getHours())
var minute = this.parsetime(date.getMinutes())
var second = this.parsetime(date.getSeconds())
var ret = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
return ret
}
}
this.parsetime = function(input){
var ret
if(input >= 0 && input < 10){
ret = '0'+input
}else{
ret = input
}
return ret
}
}
var timefriend = new timefriend('1428593779')
console.log(timefriend.get())
/*
@desc:顯示某一個時間相當(dāng)于當(dāng)前時間在多少秒前,多少分鐘前,多少小時前
@param stamp 時間戳
@param format 時間顯示格式,默認(rèn)Y-m-d H:i:s
@return 如 5秒前
*/
function timefriend($stamp,$format = 'Y-m-d H:i:s'){
if(empty($stamp)||!is_numeric($stamp)||!$stamp){
return '—';
}
$diff = time() - $stamp;
if($diff<0){
return '—';
}elseif($diff<60){
return $diff.'秒前';
}elseif($diff<3600){
return floor($diff/60).'分鐘前';
}elseif($diff<86400){
return floor($diff/3600).'小時前';
}elseif($diff<259200){
return floor($diff/86400).'天前';
}else{
return date($format,$stamp);
}
}
DELIMITER //
CREATE FUNCTION friendlyDate(sTime char(11))
RETURNS char(11)
BEGIN
if sTime='' then
return '-';
end if;
set @cTime=UNIX_TIMESTAMP();
set @dTime=@cTime-sTime;
set @dDay=DATE_FORMAT(@cTime,'%e')-DATE_FORMAT(@sTime,'%e');
set @dYear=DATE_FORMAT(@cTime,'%Y')-DATE_FORMAT(@sTime,'%Y');
if @dTime>0 then
if @dTime<60 then
if @dTime<10 then
return '剛剛';
else
set @t=floor(@dTime/10)*10;
set @ret=concat(@t,'秒前');
return @ret;
end if;
elseif @dTime<3600 then
set @t=@dTime/60;
set @ret=concat(@t,'分鐘前');
return @ret;
elseif @dYear=0 && @dDay=0 then
set @t=DATE_FORMAT(@sTime,'%H:%i');
set @ret=concat('今天',@t);
return @ret;
elseif @dYear=0 then
return DATE_FORMAT(@sTime,'%m月%d日 %H:%i');
else
return DATE_FORMAT(@sTime,'%Y-%m-%d %H:%i:%s');
end if;
else
return DATE_FORMAT(@sTime,'%Y年%m月%d日 %H:%i');
end if;
END
//
DELIMITER ;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。