0

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';   //
             }
        }  ?>
Community
  • 1
  • 1
Dan
  • 2,086
  • 11
  • 71
  • 137

5 Answers5

0

Try this one..

if(isset($to_date) && isset($from_date)){
    if((($fromTimestamp >= $from_date) && ($fromTimestamp <= $to_date)) || (($toTimestamp >= $from_date) && ($toTimestamp <= $to_date)))
    {
      $val = 'Y'; //return Y in meet this condition
    }
    else{
      $val = 'N'; // return N if meet this condition
    }
}
if($val == 'Y') //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';   //
}
Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
  • still not all paths of the function returns a value. – Mark Apr 08 '13 at 05:50
  • 1
    @UlfricStormcloak oh yea great idea .. should update this to `isset()` thanks mate. – Jhonathan H. Apr 08 '13 at 05:57
  • @Kaii Still the same error. Every time i submit date it shows the `Budget already set for this from-date & to-date` . What's wrong!! please help – Dan Apr 08 '13 at 06:10
  • 1
    you said `Try to remove unnecessary () in your if conditions` but () are not unnecessary,if i remove it is giving syntax error – Dan Apr 08 '13 at 06:27
  • @Ashutosh oh yea seen it thanks..check it now.. then update us – Jhonathan H. Apr 08 '13 at 06:29
  • Sorry! Still same problem. Is my logic is correct? Every time its showing error `Budget already set for this from-date & to-date` – Dan Apr 08 '13 at 06:34
  • Sorry!! Still same problem. I updated my new code above. Please have a look. – Dan Apr 08 '13 at 06:51
  • hmm can u give values of your variables `$fromTimestamp,$toTimestamp,$from_date,$to_date` lets take it a try here – Jhonathan H. Apr 08 '13 at 07:16
0

Did you try comparison with default mysql date format 2013/04/11 (Y/m/d) ?? If you are considering only date then you must store your data in mysql database with Y/m/d format and also you can compare with same ! it works best (y)

1) Store with MySQL Date format :

date("Y/m/d", strtotime($Date));

2) Comparing Dates

<?php  
if (isset($_POST['submit']))
{
    include '../../../include/connection.php'; 
    $frmdate=  date("Y/m/d", strtotime($_POST['frmdate']));
    $todate= date("Y/m/d", strtotime($_POST['todate'])); 
     .. so on
}
?>
Hardik Thaker
  • 3,050
  • 1
  • 27
  • 37
  • Still the same error. Every time i submit date it shows the `Budget already set for this from-date & to-date` . What's wrong!! please help – Dan Apr 08 '13 at 06:13
0

i just took a look at your code.

Your while loop tends to run only once. The return FALSE; statement is the last statement inside your while loop. Therefor if you query returns multiple results, which the loop seems to imply, you will always only fetch one result. After that run your function returns either true (when the first result is matched by the if statement) or false in any other case.

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;
}

On final note on the two statements

return TRUE;
break;

The break is not needed here, the return already breaks out of the while loop.

Hope this solves your problem!

bkwint
  • 636
  • 4
  • 9
  • Tried but. Still the same error. Every time i submit date it shows the `Budget already set for this from-date & to-date` . What's wrong!! please help` – Dan Apr 08 '13 at 06:30
0
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' and from_date >= $frmdate and to_date <= $todate")
if(mysql_num_rows($result12) > 0)
{
return true;
}
else
{
return false;
}
}


if(check_function($fromTimestamp,$toTimestamp)==TRUE)//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';   //
             }
Sandeep Kapil
  • 984
  • 8
  • 14
-1

What the format from html form? $frmdate= $_POST['frmdate'] ; $todate=$_POST['todate'] ;

and what the format from your column table? ex: DATE, VARCHAR, TIMESTAMP, etc....

rafaelphp
  • 43
  • 2
  • 10