Php Flow » Php

diffrence between two date in php

You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it’s rather easy to calculate different time periods.

  1. $date1 = “2017-03-24″;  
  2. $date2 = “2019-06-26″;  
  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);  

Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>