Compare Dates using Strtotime Function in PHP

  1. // Get the dates   
  2. $today = date("m/d/Y");   
  3. $datefrom = "10/01/2010";   
  4. $dateto = "10/12/2010";   
  5.   
  6. // Convert dates to strings   
  7. $todaystring = strtotime($today);   
  8. $datefromstring = strtotime($datefrom);   
  9. $datetostring = strtotime($dateto);   
  10.   
  11. // Compare the dates (check if today is between start and end dates)   
  12. if ($todaystring >= $datefromstring && $todaystring <= $datetostring)  
  13. {   
  14.     // Do something if today is in between   
  15.     echo "Date is ok.";   
  16. }  
  17.   
  18. else  
  19. {   
  20.     // Do something if it is not in between   
  21.     echo "Date is wrong.";   
  22. }