您好,登錄后才能下訂單哦!
這篇文章主要介紹“PHP怎么獲取指定時(shí)間段之間的年,月,天,時(shí),分,秒”,在日常操作中,相信很多人在PHP怎么獲取指定時(shí)間段之間的年,月,天,時(shí),分,秒問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PHP怎么獲取指定時(shí)間段之間的年,月,天,時(shí),分,秒”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
核心代碼:
Class Utils { /** * format MySQL DateTime (YYYY-MM-DD hh:mm:ss) 把mysql中查找出來的數(shù)據(jù)格式轉(zhuǎn)換成時(shí)間秒數(shù) * @param string $datetime */ public function fmDatetime($datetime) { $year = substr($datetime,0,4); $month = substr($datetime,5,2); $day = substr($datetime,8,2); $hour = substr($datetime,11,2); $min = substr($datetime,14,2); $sec = substr($datetime,17,2); return mktime($hour,$min,$sec,$month,$day,0+$year); } /** * * 根據(jù)倆個(gè)時(shí)間獲取倆個(gè)時(shí)間的 包含的 年,月數(shù),天數(shù),小時(shí),分鐘,秒 * @param String $start * @param String $end * @return ArrayObject */ private function diffDateTime($DateStart,$DateEnd){ $rs = array(); $sYear = substr($DateStart,0,4); $eYear = substr($DateEnd,0,4); $sMonth = substr($DateStart,5,2); $eMonth = substr($DateEnd,5,2); $sDay = substr($DateStart,8,2); $eDay = substr($DateEnd,8,2); $startTime = $this->fmDatetime($DateStart); $endTime = $this->fmDatetime($DateEnd); $dis = $endTime-$startTime;//得到倆個(gè)時(shí)間的秒數(shù) $d = ceil($dis/(24*60*60));//得到天數(shù) $rs['day'] = $d;//天數(shù) $rs['hour'] = ceil($dis/(60*60));//小時(shí) $rs['minute'] = ceil($dis/60);//分鐘 $rs['second'] = $dis;//秒數(shù) $rs['week'] = ceil($d/7);//周 $tem = ($eYear-$sYear)*12;//月份 $tem1 = $eYear-$sYear;//年 if($eMonth-$sMonth<0){//月份相減為負(fù) $tem +=($eMonth-$sMonth); }else if($eMonth==$sMonth){//月份相同 if($eDay-$sDay>=0){ $tem ++; $tem1++; } }else if($eMonth-$sMonth>0){//月份相減正負(fù) $tem1++; if($eDay-$sDay>=0){//且日期相減為正數(shù) $tem +=($eMonth-$sMonth)+1; }else{ $tem +=($eMonth-$sMonth); } } $rs['month'] = $tem; $rs['year'] = $tem1; return $rs; } }
一年多一天,返回的是2年,一個(gè)月多一天返回的是2個(gè)月,以此推......項(xiàng)目需要,才做此出來,開始我也到網(wǎng)上找這樣的例子,但大家都是把年就按365天來算,月就按30天來算,這樣算出來的結(jié)果肯定是沒用的,年有可能是366天,月有可能是31,29,28都有可能
到此,關(guān)于“PHP怎么獲取指定時(shí)間段之間的年,月,天,時(shí),分,秒”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。