注意:js时间戳是13位,php时间戳是10位,这里为大家提供一个转换函数,帮助大家掌握它们之间的转换。
之前曾为大家介绍过javascript 时间戳转换的文章,今天介绍的这个是与php 时间戳的相互转换。有兴趣的朋友,可以对比学习下。
来看今天的代码吧。
/**
* javascript 时间戳 转 php 时间戳
* by http://www.jbxue.com
*/
var nowtime = (new Date).getTime();/*当前时间戳*/
/*转换时间,计算差值*/
function comptime(beginTime,endTime){
var secondNum = parseInt((endTime-beginTime*1000)/1000);//计算时间戳差值
if(secondNum>=0&&secondNum<60){
return secondNum+'秒前';
}
else if (secondNum>=60&&secondNum<3600){
var nTime=parseInt(secondNum/60);
return nTime+'分钟前';
}
else if (secondNum>=3600&&secondNum<3600*24){
var nTime=parseInt(secondNum/3600);
return nTime+'小时前';
}
else{
var nTime = parseInt(secondNum/86400);
return nTime+'天前';
}
}
t = comptime("1324362556",nowtime);//timestamp为PHP通过ajax回传的时间戳
alert(t);
>>> 您可能感兴趣的文章:
js 时间格式与时间戳转换的例子
js时间戳格式化成日期格式的几种方法
javascript日期转换 时间戳转日期格式的代码一例