Calculate the difference between two dates


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



Copy this code and paste it in your HTML
  1. $date1 = "2008-03-15";
  2. $date2 = "2011-03-16";
  3.  
  4. $diff = abs(strtotime($date2) - strtotime($date1));
  5.  
  6. $years = floor($diff / (365*60*60*24));
  7. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  8. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
  9.  
  10. printf("%d years, %d months, %d days\n", $years, $months, $days);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.