Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I am facing an unusual problem when comparing two dates.Pleas have a look.
Database table

Warranty_renewal_date (Date type= DATE)
2010-02-12
2010-02-12
2013-06-11

My code in php
PHP
$sql4="select count(*) as Under_warrenty from machine_details where warranty_renewal_date >=".$current_date;
$result4 = mysql_query($sql4) or die('Query failed: ' . mysql_error());
if ($result4) {
  while ($row4 = mysql_fetch_assoc($result4)) {
      $totlapoll4=$row4["Under_warrenty"];
      
	    }
}

 
$sql5="select count(*) as OutOf_warrenty from machine_details where warranty_renewal_date <=".$current_date ;
$result5 = mysql_query($sql5) or die('Query failed: ' . mysql_error());
if ($result5) {
  while ($row5 = mysql_fetch_assoc($result5)) {
      $totlapoll5=$row5["OutOf_warrenty"];
      
	    }
}


In this case what i should get
$totlapoll4=1
$totlapoll5=2
But i am getting 3 and 0.
Can anyone suggest whats wrong here.?
Thanks
Posted

echo sql query and check it by execute that in MySQL

Check Date format passed from PHP it should be yyyy-MM-dd

and in first query you are using >= then in second query you should use < only instead of <= it's just for correction.

Happy Coding!
:)
 
Share this answer
 
v3
Comments
[no name] 8-May-13 2:31am    
hi Aarti,
Thanks for your reply but i have done all getting same ans in both Mysql and in PHP.
that's why i post here to find out the bug.
Aarti Meswania 8-May-13 2:38am    
check updated solution
[no name] 8-May-13 2:42am    
hey, That was not reason. its just a matter of '' quote.
Aarti Meswania 8-May-13 2:48am    
good you find it
and how? by echo/print query and then trying it in mysql - so, it show you error and you correct it :)
[no name] 8-May-13 3:05am    
return 3 and 0 and Incorrect date value: '2000' for column 'warranty_renewal_date' at row 1 message and by using with '' colon giving the correct ans with no message.
hi,
This solution code.

PHP
$sql4="select count(*) as Under_warrenty from machine_details where warranty_renewal_date >='$current_date'";
$result4 = mysql_query($sql4) or die('Query failed: ' . mysql_error());
if ($result4) {
  while ($row4 = mysql_fetch_assoc($result4)) {
      $totlapoll4=$row4["Under_warrenty"];
      
	    }
}

 
$sql5="select count(*) as OutOf_warrenty from machine_details where warranty_renewal_date <'$current_date'" ;
$result5 = mysql_query($sql5) or die('Query failed: ' . mysql_error());
if ($result5) {
  while ($row5 = mysql_fetch_assoc($result5)) {
      $totlapoll5=$row5["OutOf_warrenty"];
      
	    }
}


What a silly thing!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900