Heim  >  Artikel  >  php教程  >  时间处理,空间动态1分钟前,昨天,2天前

时间处理,空间动态1分钟前,昨天,2天前

WBOY
WBOYOriginal
2016-06-07 11:38:57927Durchsuche

时间处理类库,可以传入时间戳。自己写的一个时间处理,如手机通讯的1分钟前,2小时前,昨天,3天前,4月5日
自己写的一个时间处理,如手机通讯的1分钟前,2小时前,昨天,3天前,4月5日
可以传入时间戳

demo 演示 http://mall.hnsanx.com/yzzs/dataclass/date.html

关键代码:
var NOW = new Date(); //获得当前系统时间
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}

if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}

for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}

var dateClass = {
//===============================
// 转化时间戳
// dateClass.transdate("2015-03-30 14:16:32");
// 2015-03-30
//===============================
transdate : function(endTime){
var date=new Date();
date.setFullYear(endTime.substring(0,4));
date.setMonth(endTime.substring(5,7)-1);
date.setDate(endTime.substring(8,10));
date.setHours(endTime.substring(11,13));
date.setMinutes(endTime.substring(14,16));
date.setSeconds(endTime.substring(17,19));
return Date.parse(date)/1000;
},
//===============================
// 时间格式化输出 获得当前时间
// dateClass.formatDate(1230999938,'yyyy-MM-dd hh:mm:ss'); 1230999938为时间戳
// 2015-03-30
//===============================
formatDate : function(strTime,format) {
var format = format || 'yyyy-MM-dd hh:mm:ss';
var d = strTime ? new Date(strTime) : new Date();
return d.format(format);
},
//===============================
// 爬虫时间格式化
// dateClass.formatDate("YYYY-MM-DD H:i:s");
// 2015-03-30
//===============================
dateProcess : function(oldDate) {
//alert(isNaN(oldDate));
if(!isNaN(oldDate)) oDateSeconds = oldDate;
else{
var oDate = this.formatDate(oldDate) //时间标准格式化
var oDateSeconds = this.transdate(oDate);
}
//var oDate = this.formatDate(oldDate)

var nowStr = NOW.format('yyyy-MM-dd hh:mm:ss'); //系统当前时间格式化
var nowSeconds = this.transdate(nowStr);

//alert(nowSeconds+"和"+oDateSeconds);
if(nowSeconds > oDateSeconds) {
var ToSeconds = nowSeconds - oDateSeconds;
if( ToSeconds if( ToSeconds if(ToSeconds if(ToSeconds if(ToSeconds if(ToSeconds return ToSeconds + "秒前"; //59秒前
}else{
return Math.floor(ToSeconds/60) + "分钟前" ; //59分钟前
}
}else{
return Math.floor(ToSeconds/3600) + "小时前"; //23小时前
}
}else{
var nowHour = NOW.getHours();
if(ToSeconds>(nowHour+24)*3600) return "1天前";
else return "昨天";//昨天
}
}else {
return Math.floor(ToSeconds/86400) + "天前"; //6天前
}
}
else {
return (new Date(oldDate)).format("M月d日"); //输出12月12日
}
}else {
//alert(ToSeconds);
return (new Date(oldDate)).format("yyyy年M月d日") //输出YYYY-MM-dd
}
}else{
return "时间超前"; //待处理时间为过去的时间
}
}
}

附件 dateClass.rar ( 1.28 KB 下载:21 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn