Compare dates using strtotime


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. // Do something if today is in between
  14. echo "Date is ok.";
  15. } else {
  16. // Do something if it is not in between
  17. echo "Date is wrong.";
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.