2008年7月31日 星期四

[php]模擬sqlserver中的datediff函数

/****模擬sqlserver中的datediff函數*******
$part 類型:string
取值範圍:year,month,day,hour,min,sec
表示:要增加的日期的哪個部分
$date1,$date2 類型:timestamp
表示:要比較的兩個日期
返回 類型:數值
**************結束*(*************/

function datediff($part,$date1,$date2){
//$diff=$date2-$date1;
$year1=date("Y",$date1);
$year2=date("Y",$date2);
$month2=date("m",$date2);
$month1=date("m",$date1);
$day2=date("d",$date2);
$day1=date("d",$date1);
$hour2=date("d",$date2);
$hour1=date("d",$date1);
$min2=date("i",$date2);
$min1=date("i",$date1);
$sec2=date("s",$date2);
$sec1=date("s",$date1);
$part=strtolower($part);
$ret=0;
switch ($part) {
case "year":
$ret=$year2-$year1;
break;
case "month":
$ret=($year2-$year1)*12+$month2-$month1;
break;
case "day":
$ret=(mktime(0,0,0,$month2,$day2,$year2)-mktime(0,0,0,$month1,$day1,$year1))/(3600*24);
break;
case "hour":
$ret=(mktime($hour2,0,0,$month2,$day2,$year2)-mktime($hour1,0,0,$month1,$day1,$year1))/3600;
break;
case "min":
$ret=(mktime($hour2,$min2,0,$month2,$day2,$year2)-mktime($hour1,$min1,0,$month1,$day1,$year1))/60;
break;
case "sec":
$ret=$date2-$date1;
break;
default:
return $ret;
break;
}

return $ret;
}

【下列文章您可能也有興趣】

沒有留言: