js的时间戳和php的时间戳转换需要注意的地方

来源:互联网 发布:蝴蝶效应 知乎 编辑:程序博客网 时间:2024/06/04 19:45

由PHP传入JS处理的时间戳我说怎么老是对不上号呢,原来JS时间戳为13位,包含3位毫秒的,而PHP只有10位不包含毫秒的。看来得补补基础了。

附我的一个解决Comment发表时间的函数:

<script>
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(timestamp,nowtime);//timestamp为PHP通过ajax回传的时间戳 
alert(t);
<
/script>
0 0