PHPBuilder - Subtracts two dates



RSS Twitter
Snippets Calendars Dates

Subtracts two dates

by: Kevin Gryczan
|
August 15, 2004

Version: 2.0

Type: Function

Category: Calendars/Dates

License: Other

Description: returns the difference between two date as an integer



function diffofdays($s_date,$e_date)
{
	if(strstr($s_date,"/")) {
		$s_exp = explode("/",$s_date);
		$e_exp = explode("/",$e_date);
		$s_ts = mktime(0,0,0,$s_exp[0],$s_exp[1],$s_exp[2]);
		$e_ts = mktime(0,0,0,$e_exp[0],$e_exp[1],$e_exp[2]);
		$s_day = date("z",$s_ts);
		$e_day = date("z",$e_ts);
		if ($s_day > $e_day)
		{
			if (date("L", $s_ts) == 1)
			{
				$diff = (366 - $s_day) + $e_day;
				echo $diff;
			}
			
			else
			{
				$diff = (365 - $s_day) + $e_day;
				echo $diff;
			}
			
		}
		
		else
		{
			$diff = $e_day - $s_day;
		}
	} elseif(strstr($s_date,"-")) {
		$s_exp = explode("-",$s_date);
		$e_exp = explode("-",$e_date);
		$s_ts = mktime(0,0,0,$s_exp[1],$s_exp[2],$s_exp[0]);
		$e_ts = mktime(0,0,0,$e_exp[1],$e_exp[2],$e_exp[0]);
		$s_day = date("z",$s_ts);
		$e_day = date("z",$e_ts);
		if ($s_day > $e_day)
		{
			if (date("L", $s_ts) == 1)
			{
				$diff = (366 - $s_day) + $e_day;
				echo $diff;
			}
			
			else
			{
				$diff = (365 - $s_day) + $e_day;
				echo $diff;
			}
			
		}
		
		else
		{
			$diff = $e_day - $s_day;
		}
	} else {
		echo "Unrecognized Date Format";
	}
	return($diff);
	unset($diff);
}

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Kevin Gryczan

Comment:



Comment:

(Maximum characters: 1200). You have characters left.