Heim > php教程 > php手册 > 同步服务器端读秒

同步服务器端读秒

WBOY
Freigeben: 2016-06-07 11:35:37
Original
1422 Leute haben es durchsucht

服务器端传来时间戳,在客户端实现与服务器时间相同的时间读秒,需要引入jquery.min.js,下面直接上代码
nbsp;html><br> <br> <br>     <meta> <br>     <title></title> <br> <br> <br> <div></div> <br> <br> <script></script><br> <script><br /> $(function(){<br /> time(&#039;w&#039;,&#039;1483199990&#039;);<br /> }<br /> );<br /> <br /> /**<br /> * 读秒函数,此函数会阻塞在其后的函数,SO 这个函数需要放到所有函数最后(此函数最大动态显示到了一个月,会根据润平年判断这个月的天数)<br /> * @id {string} 存放时间的div的id名<br /> * @t {int} timestamp 服务器端传过来的时间戳,如果没有传则自动获取浏览器端的时间<br /> */<br /> function time(id,t){<br /> setTimeout(&#039;time("&#039;+id+&#039;,&#039;+t+&#039;");&#039;,1000);<br /> <br /> var dm=date(&#039;i&#039;,t);<br /> var ds=date(&#039;s&#039;,t);<br /> var dh=date(&#039;H&#039;,t);<br /> var dd=date(&#039;d&#039;,t);<br /> var dmm=date(&#039;m&#039;,t);<br /> var dy=date(&#039;Y&#039;,t);<br /> <br /> <br /> var k= $(&#039;#&#039;+id).html();<br /> if(k.length==0){<br /> $(&#039;#&#039;+id).html(&#039;<span id="y">&#039;+dy+&#039;-<span id="mm">&#039;+dmm+&#039;-<span id="d">&#039;+dd+&#039; <span id="h">&#039;+dh+&#039;:<span id="m">&#039;+dm+&#039;:<span id="s">&#039;+ds+&#039;&#039;);<br /> }<br /> <br /> var s=$(&#039;#s&#039;).html();<br /> var m=$(&#039;#m&#039;).html();<br /> var h=$(&#039;#h&#039;).html();<br /> var d=$(&#039;#d&#039;).html();<br /> var mm=$(&#039;#mm&#039;).html();<br /> var y=$(&#039;#y&#039;).html();<br /> if(s.length==0){<br /> s=0;<br /> }<br /> <br /> if(parseInt(s)==60){<br /> s=0;<br /> rm=parseInt(m)+parseInt(1);<br /> if(rm==60){<br /> rm=0;<br /> rh=parseInt(h)+parseInt(1);<br /> if(rh==24){<br /> rh=0;<br /> rd=parseInt(d)+parseInt(1);<br /> //润平年选择<br /> var big=Array(1,3,5,7,8,10,12);<br /> var small=Array(4,6,9,11);<br /> if(IsPinYer(y)){ //2月29天<br /> if(mm==2){ //2月<br /> if(rd==30){<br /> rd=1;<br /> rmm=parseInt(mm)+parseInt(1);<br /> ry=parseInt(y);<br /> }else{<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }<br /> if(InArray(mm,big)){<br /> if(rd==32){<br /> rd=1;<br /> rmm=parseInt(mm)+parseInt(1);<br /> if(rmm==13){ //跨年<br /> rmm=1;<br /> ry=parseInt(y)+parseInt(1);<br /> }else {<br /> ry = parseInt(y);<br /> }<br /> }else{<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }<br /> if(InArray(mm,small)){<br /> if(rd==31){<br /> rd=1;<br /> rmm=parseInt(mm)+parseInt(1);<br /> ry=parseInt(y);<br /> }else{<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }<br /> }else{ //2月28天<br /> if(mm==2){ //2月<br /> if(rd==29){<br /> rd=1;<br /> rmm=parseInt(mm)+parseInt(1);<br /> ry=parseInt(y);<br /> }else{<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }<br /> if(InArray(mm,big)){<br /> if(rd==32){<br /> rd=1;<br /> rmm=parseInt(mm)+parseInt(1);<br /> if(rmm==13){ //跨年<br /> rmm=1;<br /> ry=parseInt(y)+parseInt(1);<br /> }else {<br /> ry = parseInt(y);<br /> }<br /> }else{<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }<br /> if(InArray(mm,small)){<br /> if(rd==31){<br /> rd=1;<br /> rmm=parseInt(mm)+parseInt(1);<br /> ry=parseInt(y);<br /> }else{<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }<br /> }<br /> }else{<br /> rd=parseInt(d);<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }else{<br /> rh=parseInt(h);<br /> rd=parseInt(d);<br /> rmm=parseInt(mm);<br /> ry=parseInt(y);<br /> }<br /> }else{<br /> var rm=parseInt(m);<br /> var rh=parseInt(h);<br /> var rd=parseInt(d);<br /> var rmm=parseInt(mm);<br /> var ry=parseInt(y);<br /> }<br /> <br /> var v=parseInt(s)+parseInt(1);<br /> $(&#039;#&#039;+id).html(&#039;<span id="y">&#039;+ry+&#039;-<span id="mm">&#039;+rmm+&#039;-<span id="d">&#039;+rd+&#039; <span id="h">&#039;+rh+&#039;:<span id="m">&#039;+rm+&#039;:<span id="s">&#039;+v+&#039;&#039;);<br /> }<br /> <br /> <br /> <br /> function InArray(str,arr){ //判断str是否在arr这个数组中存在<br /> for(var i=0;i<arr.length;i++){<br /> if(arr[i]==str){<br /> return true;<br /> }<br /> }<br /> return false;<br /> }<br /> <br /> <br /> <br /> /**<br /> * 和PHP一样的时间戳格式化函数<br /> * @param {string} format 格式<br /> * @param {int} timestamp 要格式化的时间 默认为当前时间<br /> * @return {string} 格式化的时间字符串<br /> */<br /> function date(format, timestamp){<br /> var a, jsdate=((timestamp) ? new Date(timestamp*1000) : new Date());<br /> var pad = function(n, c){<br /> if((n = n + "").length < c){<br /> return new Array(++c - n.length).join("0") + n;<br /> } else {<br /> return n;<br /> }<br /> };<br /> var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];<br /> var txt_ordin = {1:"st", 2:"nd", 3:"rd", 21:"st", 22:"nd", 23:"rd", 31:"st"};<br /> var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];<br /> var f = {<br /> // Day<br /> d: function(){return pad(f.j(), 2)},<br /> D: function(){return f.l().substr(0,3)},<br /> j: function(){return jsdate.getDate()},<br /> l: function(){return txt_weekdays[f.w()]},<br /> N: function(){return f.w() + 1},<br /> S: function(){return txt_ordin[f.j()] ? txt_ordin[f.j()] : &#039;th&#039;},<br /> w: function(){return jsdate.getDay()},<br /> z: function(){return (jsdate - new Date(jsdate.getFullYear() + "/1/1")) / 864e5 >> 0},<br /> <br /> // Week<br /> W: function(){<br /> var a = f.z(), b = 364 + f.L() - a;<br /> var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1;<br /> if(b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b){<br /> return 1;<br /> } else{<br /> if(a <= 2 && nd >= 4 && a >= (6 - nd)){<br /> nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31");<br /> return date("W", Math.round(nd2.getTime()/1000));<br /> } else{<br /> return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);<br /> }<br /> }<br /> },<br /> <br /> // Month<br /> F: function(){return txt_months[f.n()]},<br /> m: function(){return pad(f.n(), 2)},<br /> M: function(){return f.F().substr(0,3)},<br /> n: function(){return jsdate.getMonth() + 1},<br /> t: function(){<br /> var n;<br /> if( (n = jsdate.getMonth() + 1) == 2 ){<br /> return 28 + f.L();<br /> } else{<br /> if( n & 1 && n < 8 || !(n & 1) && n > 7 ){<br /> return 31;<br /> } else{<br /> return 30;<br /> }<br /> }<br /> },<br /> <br /> // Year<br /> L: function(){var y = f.Y();return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0},<br /> //o not supported yet<br /> Y: function(){return jsdate.getFullYear()},<br /> y: function(){return (jsdate.getFullYear() + "").slice(2)},<br /> <br /> // Time<br /> a: function(){return jsdate.getHours() > 11 ? "pm" : "am"},<br /> A: function(){return f.a().toUpperCase()},<br /> B: function(){<br /> // peter paul koch:<br /> var off = (jsdate.getTimezoneOffset() + 60)*60;<br /> var theSeconds = (jsdate.getHours() * 3600) + (jsdate.getMinutes() * 60) + jsdate.getSeconds() + off;<br /> var beat = Math.floor(theSeconds/86.4);<br /> if (beat > 1000) beat -= 1000;<br /> if (beat < 0) beat += 1000;<br /> if ((String(beat)).length == 1) beat = "00"+beat;<br /> if ((String(beat)).length == 2) beat = "0"+beat;<br /> return beat;<br /> },<br /> g: function(){return jsdate.getHours() % 12 || 12},<br /> G: function(){return jsdate.getHours()},<br /> h: function(){return pad(f.g(), 2)},<br /> H: function(){return pad(jsdate.getHours(), 2)},<br /> i: function(){return pad(jsdate.getMinutes(), 2)},<br /> s: function(){return pad(jsdate.getSeconds(), 2)},<br /> //u not supported yet<br /> <br /> // Timezone<br /> //e not supported yet<br /> //I not supported yet<br /> O: function(){<br /> var t = pad(Math.abs(jsdate.getTimezoneOffset()/60*100), 4);<br /> if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t;<br /> return t;<br /> },<br /> P: function(){var O = f.O();return (O.substr(0, 3) + ":" + O.substr(3, 2))},<br /> //T not supported yet<br /> //Z not supported yet<br /> <br /> // Full Date/Time<br /> c: function(){return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P()},<br /> //r not supported yet<br /> U: function(){return Math.round(jsdate.getTime()/1000)}<br /> };<br /> <br /> return format.replace(/[\\]?([a-zA-Z])/g, function(t, s){<br /> if( t!=s ){<br /> // escaped<br /> ret = s;<br /> } else if( f[s] ){<br /> // a date function exists<br /> ret = f[s]();<br /> } else{<br /> // nothing special<br /> ret = s;<br /> }<br /> return ret;<br /> });<br /> }<br /> <br /> <br /> <br /> function IsPinYer(yer){ //判断润平年 返回真2月29天<br /> return(0==yer%4 && (yer%100!=0 || yer%100==0));<br /> }<br /> </script><br>

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

Quelle:php.cn
Erklärung dieser Website
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
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage