How to calculate the difference between two dates using PHP?

Answer

For PHP < 5.3 otherwise see jurka's answer below

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.

$date1 ="2007-03-24";
$date2 ="2009-06-26";

$diff = abs(strtotime($date2)- strtotime($date1));

$years = floor($diff /(365*60*60*24));
$months = floor(($diff - $years *365*60*60*24)/(30*60*60*24));
$days = floor(($diff - $years *365*60*60*24- $months*30*60*60*24)/(60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

All php Questions

Ask your interview questions on php

Write Your comment or Questions if you want the answers on php from php Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---