Comparing date type variables as form inputs in PHP

I was working with a search module where I needed to work upon the user “date” inputs. The form inputs of dates were string type like “2011-05-06”. I had to confirm few things:
— if both of the input fields [said as $var6 and $var7] are blank or not.
— if first one [$var6] is filled only
— if second date [$var7] is filled only
— user can give two dates in any order. BUT MYSQL ‘BETWEEN’ command [which I used to compare two dates] expects the first argument as early number and second one as big number. so I had to swap two numbers in proper order
here goes some additional needless info,
$var6 = @$_GET[‘date_1’] ;
$var7 = @$_GET[‘date_2’] ;
$wh = “WHERE”; //initializing variable
$cond6 = “”; // initializing variable

// this is for date variables var6 and var7 is date variable
		if($var6 == "0000-00-00") //as blank inputs came in this format, I made them NULL
		{
			$var6 = "";
			}
			if($var7 == "0000-00-00")
		{
			$var7 = "";
			}
	
	
	if($var6 <> "") // input is given 
	{	
		if($var7 <> "") // input is given
		{ 
			if($var6 < $var7)
			{	
				$cond6 = $wh." date BETWEEN "."'".$var6."'"." AND "."'".$var7."'";
				}
				
			if($var6 == $var7)
			{
				$cond6 = $wh." date = "."'".$var6."'";
				}
				
			if($var6 > $var7)
			{
				$var8 = $var7;
				$var9 = $var6;
				$cond6 = $wh." date BETWEEN "."'".$var8."'"." AND "."'".$var9."'";
				}
		}
			
			if($var7 == "")
				{
					$cond6 = $wh." date = "."'".$var6."'";
					}
	}
		
		if($var6 = "")
	
		{
			if($var7 <> "")
			{
				$cond6 = $wh." date = "."'".$var7."'";
				}

			if($var7 == "")
			{
				$cond6 = "";
				}
			}
				

PS: $cond6 is the variable here which I used to make a part of a whole SQL string.
Whole string was like:

$query = "SELECT * from Mahmud WHERE".$condition;

Here goes the $condition which I used:

$condition = $cond1.$cond2.$cond3.$cond4.$cond5.$cond6;

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s