<?php
function CalculateTimeMoney($typeunit,&$db,$userid,$price,$limithour,$price1,$starttime,$maxmoney)
{
	$allmoney=0;
	if ($typeunit==2) {		//小时，每天封顶
		$time1=strftime("%Y-%m-%d 00:00:00",strtotime($starttime));
		$time2=strftime("%Y-%m-%d 23:59:59",strtotime($starttime));
		//每天时间范围从0点到24点前
		while ( strtotime($time1)<time() ) {		//依次检查每天的上网记录，直到当前天
			$money=0;
			$usetime=0;
			$rs = mysql_query( "select sum(facttime) from history where userid='".$userid."' and starttime>='".$time1."' and starttime<'".$time2."'",$db );
			if ($row = mysql_fetch_array($rs)) {
				$usetime=$row[0];
				if (is_null($usetime)) {
					$usetime=0;
				}
			}
			mysql_free_result($rs);
			if ($usetime>0) {
				$usetime=round($usetime/3600,3);
				$money=round($usetime*$price,3);
				if ($money>$maxmoney) {		//超过封顶
					$money=$maxmoney;
				}
			}
			$allmoney=$allmoney+$money;
			$tmptime=strtotime("+1 day",strtotime($time1));		//再加一天
			$time1=strftime("%Y-%m-%d 00:00:00",$tmptime);
			$time2=strftime("%Y-%m-%d 23:59:59",$tmptime);
		}
	}
	elseif ($typeunit==3) {		//天
		$time1=strftime("%Y-%m-%d 00:00:00",strtotime($starttime));
		$time2=strftime("%Y-%m-%d 23:59:59",strtotime($starttime));
		//每天时间范围从0点到24点前
		while ( strtotime($time1)<time() ) {		//依次检查每天的上网记录，直到当前天
			$money=0;
			$usetime=0;
			$rs = mysql_query( "select sum(facttime) from history where userid='".$userid."' and starttime>='".$time1."' and starttime<'".$time2."'",$db );
			if ($row = mysql_fetch_array($rs)) {
				$usetime=$row[0];
				if (is_null($usetime)) {
					$usetime=0;
				}
			}
			mysql_free_result($rs);
			if ($usetime>0) {
				$usetime=round($usetime/3600,3);
				if ($usetime>$limithour) {
					$money=1*$price+round(($usetime-$limithour)*$price1,3);
					if ($maxmoney>0) {		//有封顶
						if ($money>$maxmoney) {		//超过封顶
							$money=$maxmoney;
						}
					}
				}
				else {		//当天没有超过限量
					$money=1*$price;
				}
			}
			else {		//该天没有上过网，直接算1天，如果某天实际没有上网时需要排除该天，直接改money=0
				$money=1*$price;
			}
			$allmoney=$allmoney+$money;
			$tmptime=strtotime("+1 day",strtotime($time1));		//再加一天
			$time1=strftime("%Y-%m-%d 00:00:00",$tmptime);
			$time2=strftime("%Y-%m-%d 23:59:59",$tmptime);
		}
	}
	else {		//月
		$time1=$starttime;
		$tmptime=strtotime("+1 moneth",strtotime($time1));		//加一月
		$time2=strftime("%Y-%m-%d %H:%M:%S",$tmptime);
		while ( strtotime($time1)<time() ) {		//依次检查每月的上网记录，直到当前月
			$money=0;
			$usetime=0;
			$rs = mysql_query( "select sum(facttime) from history where userid='".$userid."' and starttime>='".$time1."' and starttime<'".$time2."'",$db );
			if ($row = mysql_fetch_array($rs)) {
				$usetime=$row[0];
				if (is_null($usetime)) {
					$usetime=0;
				}
			}
			mysql_free_result($rs);
			if ($usetime>0) {
				$usetime=round($usetime/3600,3);
				if ($usetime>$limithour) {
					$money=1*$price+round(($usetime-$limithour)*$price1,3);
					if ($maxmoney>0) {		//有封顶
						if ($money>$maxmoney) {		//超过封顶
							$money=$maxmoney;
						}
					}
				}
				else {		//当月没有超过限量
					$money=1*$price;
				}
			}
			else {		//直接算1月
				$money=1*$price;
			}
			$allmoney=$allmoney+$money;
			$time1=$time2;
			$tmptime=strtotime("+1 month",strtotime($time1));		//再加一月
			$time2=strftime("%Y-%m-%d 23:59:59",$tmptime);
		}
	}
	return $allmoney;
}

function CalculateFlowMoney($typeunit,&$db,$userid,$price,$limitflow,$price1,$starttime,$maxmoney)
{
	$allmoney=0;
	//小时类型无流量限制和封顶
	if ($typeunit==3) {		//天
		$time1=strftime("%Y-%m-%d 00:00:00",strtotime($starttime));
		$time2=strftime("%Y-%m-%d 23:59:59",strtotime($starttime));
		//每天时间范围从0点到24点前
		while ( strtotime($time1)<time() ) {		//依次检查每天的上网记录，直到当前天
			$money=0;
			$usedata=0;
			$rs = mysql_query( "select sum(factdata) from history where userid='".$userid."' and starttime>='".$time1."' and starttime<'".$time2."'",$db );
			if ($row = mysql_fetch_array($rs)) {
				$usedata=$row[0];
				if (is_null($usedata)) {
					$usedata=0;
				}
			}
			mysql_free_result($rs);
			if ($usedata>0) {
				$usedata=round($usedata/1024,2);
				if ($usedata>$limitflow) {
					$money=1*$price+round(($usedata-$limitflow)*$price1,3);
					if ($maxmoney>0) {		//有封顶
						if ($money>$maxmoney) {		//超过封顶
							$money=$maxmoney;
						}
					}
				}
				else {		//当天没有超过限量
					$money=1*$price;
				}
			}
			else {		//该天没有上过网，直接算1天，如果某天实际没有上网时需要排除该天，直接改money=0
				$money=1*$price;
			}
			$allmoney=$allmoney+$money;
			$tmptime=strtotime("+1 day",strtotime($time1));		//再加一天
			$time1=strftime("%Y-%m-%d 00:00:00",$tmptime);
			$time2=strftime("%Y-%m-%d 23:59:59",$tmptime);
		}
	}
	else {		//月
		$time1=$starttime;
		$tmptime=strtotime("+1 moneth",strtotime($time1));		//加一月
		$time2=strftime("%Y-%m-%d %H:%M:%S",$tmptime);
		while ( strtotime($time1)<time() ) {		//依次检查每月的上网记录，直到当前月
			$money=0;
			$usedata=0;
			$rs = mysql_query( "select sum(factdata) from history where userid='".$userid."' and starttime>='".$time1."' and starttime<'".$time2."'",$db );
			if ($row = mysql_fetch_array($rs)) {
				$usedata=$row[0];
				if (is_null($usedata)) {
					$usedata=0;
				}
			}
			mysql_free_result($rs);
			if ($usedata>0) {
				$usedata=round($usedata/1024,2);
				if ($usedata>$limitflow) {
					$money=1*$price+round(($usedata-$limitflow)*$price1,3);
					if ($maxmoney>0) {		//有封顶
						if ($money>$maxmoney) {		//超过封顶
							$money=$maxmoney;
						}
					}
				}
				else {		//当月没有超过限量
					$money=1*$price;
				}
			}
			else {		//直接算1月
				$money=1*$price;
			}
			$allmoney=$allmoney+$money;
			$time1=$time2;
			$tmptime=strtotime("+1 month",strtotime($time1));		//再加一月
			$time2=strftime("%Y-%m-%d 23:59:59",$tmptime);
		}
	}
	return $allmoney;
}

function DateDiff($opertype,$starttime,$endtime)
{
	if ($starttime>=$endtime) return 0;	//starttime在endtime之后，返回0
	$n=0;
	if ($opertype=="d") {
		if ( strftime("%Y-%m-%d",$starttime)==strftime("%Y-%m-%d",$endtime) ) return 0;	//同一天
		while (TRUE) {
			$n++;
			if ( strftime("%Y-%m-%d",strtotime("+".strval($n)." day",$starttime))==strftime("%Y-%m-%d",$endtime) ) {
				//年月日部分已经相同
				break;
			}
		}
	}
	elseif ($opertype=="m") {
		if ( strftime("%Y-%m",$starttime)==strftime("%Y-%m",$endtime) ) return 0;	//同一月
		while (TRUE) {
			$n++;
			if ( strftime("%Y-%m",strtotime("+".strval($n)." month",$starttime))==strftime("%Y-%m",$endtime) ) {
				//年月部分已经相同
				break;
			}
		}
	}
	elseif ($opertype=="yyyy") {
		if ( strftime("%Y",$starttime)==strftime("%Y",$endtime) ) return 0;	//同一年
		while (TRUE) {
			$n++;
			if ( strftime("%Y",strtotime("+".strval($n)." year",$starttime))==strftime("%Y",$endtime) ) {
				//年部分已经相同
				break;
			}
		}
	}
	elseif ($opertype=="s") {
		$n=$endtime-$starttime;
	}
	else {
		$n=0;
	}
	return $n;
}
?>
