I am comparing two date, From_date
& To_date
with My database table rows.
Logic is: if users has already set budget between these 2 dates(dates received from textboxes),then return an error message Budget already set for this from-date & to-date
.
i am converting my date into Unix Time-stamp for easier.
I refer this link before trying this: 1. how to check two dates between from-date and to-date 2. What's the most efficient way to test two integer ranges for overlap?
I tried the below code: but i am not getting result.
It always shows me the wrong message i.e Budget already set for this from-date & to-date
<?php if (isset($_POST['submit']))
{
include '../../../include/connection.php';
$frmdate= $_POST['frmdate'] ;
$todate=$_POST['todate'] ;
$fromTimestamp = strtotime( $frmdate );
$toTimestamp = strtotime( $todate );
function check_function($fromTimestamp,$toTimestamp)//function for checking the two dates which are rrecieved from the database table
{
$sid=$_SESSION['id'];
$result12=mysql_query("SELECT * FROM budget where creater_id = '$sid'");
while($test12 = mysql_fetch_array($result12))
{
$from_date = strtotime($test12['from_date']);//getting all the FROM-Dates_column
$to_date = strtotime($test12['to_date']);//getting all the TO-Dates_column
if((($fromTimestamp >= $from_date) && ($fromTimestamp <= $to_date)) || (($toTimestamp >= $from_date) && ($toTimestamp <= $to_date)))
{
return TRUE;
break;
}
return FALSE;
}
}
if(check_function($fromTimestamp,$toTimestamp))//Returns TRUE
{
mysql_query("INSERT INTO `budget` (heads_id,amount,from_date,to_date,creater_id,created_date,updater_id,updated_date) VALUES ('$headsid','$amount','$frmdate','$todate',$uid,NOW(),$uid,NOW())");
}
else
{
echo 'Budget already set for this from-date & to-date'; //
}
} ?>