PHP,JavaScript,MySQL运行时间那些关于PHP中时间的那些事PHPPHP中时间运行的单位是一秒,把时间戳转成正常的日期格式//currenttimestamptime();//当前时间格式date("Y-m-dH:i:s",time());//昨天时间格式date("Y-m-d,H:i:s",time()-3600*24);二、Convertordinarydateformattotimestamp//Convertdateformattotimestampstrtotime("now");//当前时间戳strtotime("上周一");//上周一的时间戳strtotime("+1day");//一天后的时间戳strtotime("2017-12-25");//1514131200JavaScriptjs中时间操作的单位是毫秒1.js将时间转换为时间戳js方法获取当前时间戳vartimestamp1=Date.parse(newDate());vartimestamp2=(newDate()).valueOf();vartimestamp3=newDate().getTime();第一种:获取的时间戳为毫秒改成000显示,第二种和第三种是获取当前毫秒的时间戳。js方法获取自定义时间戳varoldTime=(newDate("2017-12-2508:00:20")).getTime();getTime()返回值的单位是毫秒。2.js将时间戳转换为通用日期格式DatetoLocaleStringmethodfunctiongetLocalTime(nS){returnnewDate(parseInt(nS)).toLocaleString().replace(/:d{1,2}$/,'');}parseInt()函数可以解析一个字符串并返回一个整数。toLocaleString()方法可以根据本地时间将Date对象转换为字符串并返回结果。replace(/:d{1,2}$/,'')验证并替换为:以一个或两个数字的结尾字符串开头,即秒;替换为空的Date属性方法functionadd0(m){returnm<10?'0'+m:m}functionformat(shijianchuo){//shijianchuo为整数,否则parseInt转换vartime=newDate(shijianchuo);vary=time.getFullYear();varm=time.getMonth()+1;vard=time.getDate();varh=time.getHours();varmm=time.getMinutes();vars=time.getSeconds();返回y+'-'+add0(m)+'-'+add0(d)+''+add0(h)+':'+add0(mm)+':'+add0(s);}3.封装时间格式化器/***和类PHP时间戳格式化函数*@param{string}formatformat*@param{int}timestamp需要格式化的时间默认为当前时间*@return{string}格式化时间字符串*/functiondate(format,timestamp){vara,jsdate=((timestamp)?newDate(timestamp*1000):newDate());varpad=function(n,c){if((n=n+"").length>0},//周W:function(){vara=f.z(),b=364+f.L()-a;varnd2,nd=(newDate(jsdate.getFullYear()+"/1/1").getDay()||7)-1;if(b<=2&&((jsdate.getDay()||7)-1)<=2-b){返回1;}别的{如果(a<=2&&nd>=4&&a>=(6-nd)){nd2=newDate(jsdate.getFullYear()-1+"/12/31");返回日期("W",Math.round(nd2.getTime()/1000));}else{return(1+(nd<=3?((a+nd)/7):(a-(7-nd))/7)>>0);}}},//月F:function(){returntxt_months[f.n()]},m:function(){returnpad(f.n(),2)},M:function(){returnf.F().substr(0,3)},n:function(){returnjsdate.getMonth()+1},t:function(){varn;if((n=jsdate.getMonth()+1)==2){返回28+f.L();}else{if(n&1&&n<8||!(n&1)&&n>7){返回31;}else{返回30;}}},//年L:函数(){vary=f.Y();返回(!(y&3)&&(y%1e2||!(y%4e2)))?1:0},//o尚不支持Y:function(){returnjsdate.getFullYear()},y:function(){return(jsdate.getFullYear()+"").slice(2)},//时间a:function(){returnjsdate.getHours()>11?"pm":"am"},A:function(){returnf.a().toUpperCase()},B:function(){//peterpaulkoch:varoff=(jsdate.getTimezoneOffset()+60)*60;vartheSeconds=(jsdate.getHours()*3600)+(jsdate.getMinutes()*60)+jsdate.getSeconds()+off;varbeat=Math.floor(theSeconds/86.4);如果(节拍>1000)节拍-=1000;如果(节拍<0)节拍+=1000;如果((String(beat)).length==1)beat="00"+beat;如果((String(beat)).length==2)beat="0"+beat;返回节拍;},g:function(){返回jsdate.getHours()%12||12},G:函数n(){returnjsdate.getHours()},h:function(){returnpad(f.g(),2)},H:function(){returnpad(jsdate.getHours(),2)},i:function(){returnpad(jsdate.getMinutes(),2)},s:function(){returnpad(jsdate.getSeconds(),2)},//你还不支持//时区//e不支持yet//我还不支持O:function(){vart=pad(Math.abs(jsdate.getTimezoneOffset()/60*100),4);如果(jsdate.getTimezoneOffset()>0)t="-"+t;否则t="+"+t;返回吨;},P:function(){varO=f.O();return(O.substr(0,3)+":"+O.substr(3,2))},//T暂不支持//Z尚不支持//完整日期/时间c:function(){returnf.Y()+"-"+f.m()+"-"+f.d()+"T"+f.h()+":"+f.i()+":"+f.s()+f.P()},//r尚不支持U:function(){returnMath.round(jsdate.getTime()/1000)}};返回格式.replace(/[\\]?([a-zA-Z])/g,函数(t,s){if(t!=s){//转义ret=s;}elseif(f[s]){//日期函数存在ret=f[s]();}else{//没什么特别的ret=s;返回ret;});}调用方法可以方便地将时间戳(秒)转换成日期格式,如:date('Y-m-d','1514093093');转换为2017-12-24date('Y-m-dH:i:s','1514093110');转换为2017-12-2413:25:10MySQLMySQL的时间单位是秒一,普通时间日期格式化相关函数获取当前普通时间Format#Getthecurrenttimeanddateselectnow();#2017-12-2414:06:12#当前日期选择curdate();#2017-12-24#当前时间选择curtime();#14:06:12获取指定时间的部分#返回日期时间的日期部分date()#返回日期的时间部分time()#返回日期的年份部分year()#给定一个date日期,返回天数(从0年开始的天数)TO_DAYS()#求两个日期的差,返回天数datediff('20171225','20170101')datediff('2017-12-25','2017-01-01')时间间隔#将时间添加到指定的日期间隔。date_add('20171224',间隔1天);#return2017-12-25#指定日期减去时间间隔后的具体日期date_sub('20171224',interval9day)#fReturn2017-12-15#返回指定日期为全年的第几天dayofyear(date):2.将时间戳转为通用的日期时间格式#currenttimestampunix_timestamp()#将时间戳转为日期时间格式selectFROM_UNIXTIME(1514131200);#2017-12-2500:00:00