PHP: How to calculate the difference between two given dates

0   0
Nitin Sharma
profile Ranjeet 8th November 2016
<?php
 
 
 
$startdate = "20016-03-24"; //date1
$enddate = "20016-06-27"; //date2
 
$difference  = abs(strtotime($enddate) - strtotime($startdate));
 
$years = floor($difference / (365*60*60*24));
$months = floor(($difference - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($difference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
 
printf("%d years, %d months, %d days\n", $years, $months, $days);
?>
Write a comment ...
Post comment
Cancel

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site