Posted By

dsoms on 03/17/11


Tagged

php difference dates


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

skywalker


Calculate the difference between two dates


 / Published in: PHP
 

  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  

You need to login to post a comment.