《PHP學(xué)習(xí):PHP獲取指定時(shí)間段之間的 年,月,天,時(shí),分,秒》要點(diǎn):
本文介紹了PHP學(xué)習(xí):PHP獲取指定時(shí)間段之間的 年,月,天,時(shí),分,秒,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP編程核心代碼:
PHP編程
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;
}
}
PHP編程一年多一天,返回的是2年,一個(gè)月多一天返回的是2個(gè)月,以此推......項(xiàng)目需要,才做此出來,開始我也到網(wǎng)上找這樣的例子,但大家都是把年就按365天來算,月就按30天來算,這樣算出來的結(jié)果肯定是沒用的,年有可能是366天,月有可能是31,29,28都有可能
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/6266.html