In this tutorial, We will learn how to calculate the difference between two dates in PHP? I mean the difference between two dates in days, months, years using PHP.


Example:

<?php
  
$startDate = "2019-05-20";
$endDate = "2021-08-27";
  
$diffData = abs(strtotime($endDate) - strtotime($startDate));
   
$yearsDiff = floor($diffData / (365*60*60*24));
print_r("Years:".$yearsDiff);
   
$monthsDiff = floor(($diffData - $yearsDiff * 365*60*60*24) / (30*60*60*24));
print_r(" Months:".$monthsDiff);
   
$daysDiff = floor(($diffData - $yearsDiff * 365*60*60*24 - $monthsDiff*30*60*60*24)/ (60*60*24));
print_r(" Days:".$daysDiff);


Output:

Years:2 Months:3 Days:10